Search code examples
jenkinsjenkins-pipelinejenkins-declarative-pipeline

Merge jenkinsfile | withcredentials and sshagent


I am trying to execute ansible playbook in all EC2 AWS instances using Jenkinsfile function and assume-role. But I am getting below error.

Obtained devops/JenkinsfileDynamic from git [email protected]:tui-uk-dev/cng-airflow-dags.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 33: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 33, column 134.
   SION_TOKEN=${AWS_SESSION_TOKEN} AWS_DEFA
                                 ^

Jenkinsfile:-

    def Host_Verification2() {
    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'cant_be_disclosed']]) {
         sh '''
          aws sts assume-role --role-arn "arn:aws:iam::12345678901:role/cant_role_jenkins" --role-session-name "connect" > assume-role-output.txt
          export AWS_ACCESS_KEY_ID=`cat assume-role-output.txt | jq -c '.Credentials.AccessKeyId' | tr -d '"' | tr -d ' '`
          export AWS_SECRET_ACCESS_KEY=`cat assume-role-output.txt | jq -c '.Credentials.SecretAccessKey' | tr -d '"' | tr -d ' '`
          export AWS_SESSION_TOKEN=`cat assume-role-output.txt | jq -c '.Credentials.SessionToken' | tr -d '"' | tr -d ' '`
          rm assume-role-output.txt
            sshagent(credentials: ['tuiuki-cng-dev']) {
                sh '''
                    cd acm/
                          sudo AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" AWS_SESSION_TOKEN="${AWS_SESSION_TOKEN}" inventory/ec2.py --list --refresh-cache
                          sudo AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" AWS_SESSION_TOKEN="${AWS_SESSION_TOKEN}" AWS_DEFAULT_REGION="eu-central-1" ansible-playbook -i inventory/ec2.py plays/emr/find.yml

                    '''
                    }
         '''
    }
}

AWS Jenkins credential


Solution

  • Like the exception is saying:

    solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"
    

    Try this:

    sudo AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"  ansible-playbook -i inventory/ec2.py plays/emr/findplaybooks.yml