Home > Enterprise >  Jenkins Ansible Tower deploy fails if passing multiple custom arguments within "extraVars"
Jenkins Ansible Tower deploy fails if passing multiple custom arguments within "extraVars"

Time:12-23

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?

CodePudding user response:

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.

CodePudding user response:

Although the Ansible Playbook plugin accepts Map type inputs (which would be preferred), this Ansible Tower plugin does demand YAML or JSON formatted strings. Your specific issue is that the YAML formatting for the string is invalid. Therefore, the easiest path forward here would be to use the writeYaml step method from the Pipeline Utility Steps plugin to convert a Map to a YAML formatted String as expected:

extraVars: writeYaml(returnText: true, data: [
  'BRANCH_NAME': env.GIT_BRANCH,
  'targeted_hosts': env.targeted_hosts
]
  • Related