jenkinsansiblejenkins-groovyansible-templateansible-awx

Jenkins Ansible Tower deploy fails if passing multiple custom arguments within "extraVars" parameter


I have a situation where I need to pass the branch name and host name (corresponds to instance w/ connection properties) into an Ansible template that is run as part of the code deployment stage. However I get the following if I try to specify multiple arguments in extraVars:

ERROR: Unable to request job template invocation Extra vars are bad: ["Cannot parse as JSON (error: Expecting value: line 1 column 1 (char 0)) or YAML (error: while parsing a block mapping\n in \"<unicode string>\", line 2, column 23:\n
  BRANCH_NAME: 'A-BranchName',\n               ^\nexpected <block end>, but found ','\n in \"<unicode string>\", line 2, column 56: \n      .... RANCH_NAME: 'A-Branch',\n              ^)."]

I am using this plugin for the call Jenkins Ansible Tower Plugin API

Here is the corresponding tower call

    stage("Ansible Deployment"){
  steps{
      script{
      ansibleTower(
        towerServer: 'Prod Tower',
        towerCredentialsId: '',
        templateType: 'job',
        jobTemplate: 'Simple Test',
        towerLogLevel: 'full',
        inventory: 'Demo Inventory',
        removeColor: false,
        verbose: true,
        extraVars: """---
              BRANCH_NAME: '${env.GIT_BRANCH}',
              targeted_hosts: '${env.targeted_hosts}',
                  """
    )
  }
 }
}

I've checked extensively online for a solution (including but not limited to here: How to pass extra variables to an Ansible playbook but just about every example I've seen (on SO and elsewhere) only deals with a single parameter (and I need to escape the values as they are set based on the branch itself). I've tried the following (based on the JSON format used here 16.13. Extra Variables:

extraVars: {
              BRANCH_NAME:'${env.GIT_BRANCH}',
              targeted_hosts:'${env.targeted_hosts}',
           }
    )

Then Jenkins will throw a groovy compiler error due to the comma. Subsequently removing the comma will throw a groovy compiler error due to the colon. Further removing that and adding it as part of the argument name string triggers a ClassCastException. The same happens when not using JSON format.

If I only specify one argument it does work at that point (in the non JSON format as shown in my original call). What else am I missing here?


Solution

  • Try either valid JSON

      extraVars: '''
    {
      "BRANCH_NAME": "${env.GIT_BRANCH}",
      "targeted_hosts": "${env.targeted_hosts}"
    }'''
    

    or valid YAML

      extraVars: '''---
    BRANCH_NAME: ${env.GIT_BRANCH}
    targeted_hosts: ${env.targeted_hosts}'''
    

    Validate the content inside the triple quotes.