# Running ansible from jenkins execute shell

**URL:** <https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291>\
**Category:** Ansible Project\
**Created:** [September 20, 2014, 8:40am UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291 "2014-09-20T08:40:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naween\_Ghimire](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@Naween\_Ghimire](https://forum.ansible.com/u/Naween_Ghimire)\
**Post date:** [September 20, 2014, 8:40am UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/1 "2014-09-20T08:40:46Z")

</div>

Hi All,

I am trying to run an ansible playbook from jenkins build to have jenkins trigger my ansible execution.

As part of my ansible-playbook run i need to pass extra-vars to the playbook during execution.

eg:

ansible-playbook -i $inventory-file $role-name --extra-vars"var1=$var1-value, var2=$var2-value"

when i put this line in the execute shell of jenkins the double quotes are evaluated and so aren’t available to the ansible-playbook command, so i escaped them as follows:

ansible-playbook -i $inventory-file $role-name --extra-vars"var1=$var1-value, var2=$var2-value"

But if i do this,

shell adds extra quotes around it as below:

ansible-playbook -i --extra-vars ’ "var1=var1-value,’ var2=var2-value, 'var3=var3-value " ’

And ansible give the following error:

Traceback (most recent call last):

File “/usr/local/bin/ansible-playbook”, line 317, in

sys.exit(main(sys.argv[1:]))

File “/usr/local/bin/ansible-playbook”, line 148, in main

extra\_vars = utils.combine\_vars(extra\_vars, utils.parse\_kv(extra\_vars\_opt))

File “/Library/Python/2.7/site-packages/ansible/utils/ **init**.py”, line 621, in parse\_kv

vargs = split\_args(args)

File “/Library/Python/2.7/site-packages/ansible/utils/splitter.py”, line 148, in split\_args

raise Exception(“error while splitting arguments, either an unbalanced jinja2 block or quotes”)

Exception: error while splitting arguments, either an unbalanced jinja2 block or quotes

Has anyone come across such a situation before ?

---

<div class="post-metadata">

**Author:** ![Baraa\_Basata1](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@Baraa\_Basata1](https://forum.ansible.com/u/Baraa_Basata1)\
**Post date:** [September 20, 2014, 5:54pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/2 "2014-09-20T17:54:31Z")

</div>

I use ansible-playbook within “Execute shell” steps and I have had no issues. Here’s what I have in the “Execute shell” step:

ansible-playbook -i inventory playbook.yml --extra-vars=“var1=${VALUE1} var2=${VALUE2}”

---

<div class="post-metadata">

**Author:** ![Damir\_Suleymanov](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Damir\_Suleymanov](https://forum.ansible.com/u/Damir_Suleymanov)\
**Post date:** [September 20, 2014, 9:24pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/3 "2014-09-20T21:24:28Z")

</div>

You can put all your variables in a YAML file and pass it as --extra-vars=“@my\_vars.yml”

Given your example, your command will look like this:  
ansible-playbook -i $inventory-file $role-name --extra-vars=“@my\_vars.yml”

and my\_vars.yml should contain:

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 21, 2014, 7:34pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/4 "2014-09-21T19:34:41Z")

</div>

What version of Ansible are you using?

---

<div class="post-metadata">

**Author:** ![jno](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jno](https://forum.ansible.com/u/jno)\
**Post date:** [November 15, 2014, 1:15pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/5 "2014-11-15T13:15:36Z")

</div>

Hi,

Sorry for hi-jacking this old thread but I’m experiencing exactly the same problems as the original poster Naween. Have also tried Damirs workaround without success, just get the same result as when not using a vars file.

I’m running the following versions of Ansible and other related software:

OS: CentOS 6.5  
Ansible verision:ansible-1.7.2-2.el6.noarch (from epel repo)  
Python Jinja version: python-jinja2-2.2.1-1.el6.rf.x86\_64 (think it was from rpmbone because of dependency trouble with other jinjas)  
Jenkins version: jenkins-1.499-1.1.noarch

I tried this with ansible-1.7.0-1.el6.noarch also from epel repo with the same result. Everything works great with dynamic extra-vars variables from the command line but not from Jenkins.

Best regards,

//Jon

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 15, 2014, 10:23pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/6 "2014-11-15T22:23:48Z")

</div>

What does your execute shell look like with Damir's workaround? Were  
you also able to see the values of the substitution has extra quotes  
in that case or is it the same traceback but not necessarily the same  
place that quotes are being mismatched?

-Toshio

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 17, 2014, 8:53pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/7 "2014-11-17T20:53:38Z")

</div>

I’m not sure I’m following, can you clarify exactly what your issue is?

The start of this thread was about how to pass variables.

---

<div class="post-metadata">

**Author:** ![jno](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jno](https://forum.ansible.com/u/jno)\
**Post date:** [November 18, 2014, 1:52pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/8 "2014-11-18T13:52:57Z")

</div>

Hi,

Sorry for being unclear about my problem. The issue is still about passing variables from Jenkins to Ansible as --extra-vars. I have done some more testing and biggest part of the problem was that I was trying to build up the complete ansible-playbook command from variables in Jenkins including extra-vars. Now when I just tried pasting the complete raw ansible-playbook command line in the Jenkins execute-shell box directly, it works just fine.

For example this works:  
/usr/bin/python2 -u ansible-playbook manage\_service.yml -l ${hosts} --extra-vars “service=${service} manage=${manage} serial=${serial} secs=${secs}”

But all the following examples causes trouble:  
cmd=“/usr/bin/python2 -u ansible-playbook manage\_service.yml -l ${hosts} --extra-vars “service=${service} manage=${manage} serial=${serial} secs=${secs}””  
cmd=“/usr/bin/python2 -u ansible-playbook manage\_service.yml -l ${hosts} --extra-vars "service=${service} manage=${manage} serial=${serial} secs=${secs}"”  
cmd=“/usr/bin/python2 -u ansible-playbook manage\_service.yml -l ${hosts} --extra-vars ‘service=${service} manage=${manage} serial=${serial} secs=${secs}’”

I also tried the workaround by using --extra-vars “@manage\_service\_vars.yml” but that produced a failure with the following message which seems to indicate that the variable does’nt get expanded:

```auto
msg: value of state must be one of: running,started,stopped,restarted,reloaded, got: $manage

```

I have also tried:  
cmd=$(/usr/bin/python2 -u ansible-playbook manage\_service.yml -l ${hosts} --extra-vars “service=${service} manage=${manage} serial=${serial} secs=${secs}”)  
Which works but you get all the output from ansible in one line.

The reason for still building the complete command as a variable is to be able to add even more parameters to ansible as options to the Jenkins job without having the complete command line in several different sections of the execute-shell code.

For example:  
if [“${debug}” == “true”]; then  
runcmd=“${cmd} -vvvv”  
else  
runcmd=“${cmd}”  
fi

It would be great if someone more knowledgeable than me 🙂 could give some input on how to solve the problem with not being able to build up the complete ansible-playbook command as a variable without breaking the --extra-vars parameter?

Best regards,

//Jon

---

<div class="post-metadata">

**Author:** ![Michael\_Schultz](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Michael\_Schultz](https://forum.ansible.com/u/Michael_Schultz)\
**Post date:** [November 18, 2014, 4:38pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/9 "2014-11-18T16:38:28Z")

</div>

It might help to split each --extra-vars into a separate args (you can have multiple --extra-vars in a single command). This should make it simpler to reason about quoting rules (in my opinion).

Our build script ends up looking something like this (hosts, site\_rev, site\_count) come from jenkins params:

args=“–extra-vars site\_rev=$site\_rev”  
args=“${args} --extra-vars site\_count=$site\_count”  
args=“${args} --limit localhost,$hosts”

ansible-playbook deploy\_site.yml ${args}

This avoids having to worry about quoting the variables for ansible itself.

–michael

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [November 18, 2014, 5:42pm UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/10 "2014-11-18T17:42:27Z")

</div>

I’d recommend using bash arrays:

args=(  
–extra-vars “site=$site”  
–extra-vars “service=$service”  
)  
args+=( -l “$hosts” )

if [-n “$just\_joking”]; then  
args+=( --check )  
fi

ansible-playbook “${args[@]}” # this is magic to stop bash from mangling the args

This is much more robust, and can even survive spaces in extra vars easily 🙂

Michael Schultz [mjschultz@gmail.com](mailto:mjschultz@gmail.com) napisał:

---

<div class="post-metadata">

**Author:** ![Greg\_Andrews](https://avatars.discourse-cdn.com/v4/letter/g/6bbea6/32.png) [@Greg\_Andrews](https://forum.ansible.com/u/Greg_Andrews)\
**Post date:** [November 20, 2014, 2:51am UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/11 "2014-11-20T02:51:54Z")

</div>

To capitalize on Tomasz’s information, there’s a way to get bash to prefix the boilerplate “–extra-vars” string to each extra arg for you:

extra\_args=( “site=$site” “service=$service” )

ansible-playbook “${extra\_args[@]/#/–extra-args }”

(note the space character between “args” and “}”)

A short example:

#!/bin/bash

site=one

service=two  
extra\_args=( “site=$site” “service=$service” )

echo ansible-playbook “${extra\_args[@]/#/–extra-args }”

will output:

ansible-playbook --extra-args site=one --extra-args service=two

There’s a similar syntax to add a suffix on each element of an array.

I haven’t used this particular function on bash versions earlier than 4.2, so be sure to test that it will work on your machine.

-Greg

---

<div class="post-metadata">

**Author:** ![jno](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jno](https://forum.ansible.com/u/jno)\
**Post date:** [November 25, 2014, 4:46am UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/12 "2014-11-25T04:46:35Z")

</div>

Many thanks to you all for the nice working solutions! You never get too old to learn new stuff and improving your Bash skills 🙂

//Jon

---

<div class="post-metadata">

**Author:** ![Matthew\_Martin](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Matthew\_Martin](https://forum.ansible.com/u/Matthew_Martin)\
**Post date:** [March 3, 2015, 7:42am UTC](https://forum.ansible.com/t/running-ansible-from-jenkins-execute-shell/16291/13 "2015-03-03T07:42:47Z")

</div>

Did you get a final resolution on this? I’ve tried several suggestions here to no avail.

Strangely, when I copy the command line output from jenkins log and run it locally, it works fine! Only when running within jenkins does it fail…

Is jenkins adding some special characters that aren’t evident?
