# calling external program and get output into a variable ?

**URL:** <https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935>\
**Category:** Ansible Project\
**Created:** [September 27, 2012, 7:07pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935 "2012-09-27T19:07:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 7:07pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/1 "2012-09-27T19:07:29Z")

</div>

Hi.

is there a way to call a python program ( or any other script) and get the output on a variable ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 27, 2012, 7:12pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/2 "2012-09-27T19:12:28Z")

</div>

Absolutely.

See 'register' in the playbook or advanced playbook docs.

- action: command /usr/bin/foo  
&nbsp;&nbsp;register: output\_var

Now you can use ${output\_var.stdout} and ${output\_var.rc} etc

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 8:32pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/3 "2012-09-27T20:32:31Z")

</div>

mmm I seem to have a problem with it.

I have this simple python program :

import easygui as eg  
import sys

dirselected = eg.diropenbox( “Select the directory”, “Select your code folder”)  
print dirselected

Then I rum my playbook and it does this :

> EXEC mkdir -p /tmp/ansible-1348777248.45-142411373373698 && chmod a+rx /tmp/ansible-1348777248.45-142411373373698 && echo /tmp/ansible-1348777248.45-142411373373698  
> REMOTE\_MODULE command python directoryselection.py  
> PUT /var/folders/Qe/QeNdcg29HPqHD66XBLccbU+++TI/-Tmp-/tmpqSbSHB TO /tmp/ansible-1348777248.45-142411373373698/command  
> EXEC chmod a+rx /tmp/ansible-1348777248.45-142411373373698/command  
> EXEC sudo -u jamengual -s /tmp/ansible-1348777248.45-142411373373698/command  
> EXEC rm -rf /tmp/ansible-1348777248.45-142411373373698/  
> ok: [localhost] =\> {“changed”: true, “cmd”: [“python”, “directoryselection.py”], “delta”: “0:00:11.392053”, “end”: “2012-09-27 13:20:59.943390”, “rc”: 0, “start”: “2012-09-27 13:20:48.551337”, “stderr”: “”, “stdout”: “/Users/jamengual/Sites”}

so I think that is actually storing the output “/Users/jamengual/Sites” in the register, but when I use that variable “shared\_folder” in another part playbook I get this :

ASK: [Creating shared folder if doesn’t exist] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
 EXEC mkdir -p /tmp/ansible-1348777260.56-266741542297489 && chmod a+rx /tmp/ansible-1348777260.56-266741542297489 && echo /tmp/ansible-1348777260.56-266741542297489  
 REMOTE\_MODULE file state=directory path={u’changed’: True, u’end’: u’2012-09-27 13:20:59.943390’, u’stdout’: u’/Users/jamengual/Sites’, u’cmd’: [u’python’, u’directoryselection.py’], u’rc’: 0, u’start’: u’2012-09-27 13:20:48.551337’, u’stderr’: u’‘, u’delta’: u’0:00:11.392053’, ‘invocation’: {‘module\_name’: ‘command’, ‘module\_args’: u’python directoryselection.py’}}  
 PUT /var/folders/Qe/QeNdcg29HPqHD66XBLccbU+++TI/-Tmp-/tmpxeWrac TO /tmp/ansible-1348777260.56-266741542297489/file  
 EXEC chmod a+rx /tmp/ansible-1348777260.56-266741542297489/file  
 EXEC sudo -u jamengual -s /tmp/ansible-1348777260.56-266741542297489/file  
 EXEC rm -rf /tmp/ansible-1348777260.56-266741542297489/  
failed: [localhost] =\> {“failed”: true, “msg”: “this module requires key=value arguments”}

So I don’t know if I’m going something wrong in my python program or the register is not getting the value ?

My Playbook looks like this :

- name: Selecting code folder to share with VM  
action: command python directoryselection.py  
register: shared\_folder

…  
…

- name: Creating shared folder if doesn’t exist  
action: file state=directory path=$shared\_folder

Thanks.

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 9:09pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/4 "2012-09-27T21:09:13Z")

</div>

Ohhh…I’m getting a dictionary with the results…now is where my python abilities do not play well…

How can I get the value of the stdout in a variable in ansible ?

Thanks.

P.S: I know that this might be a python question, sorry for being so annoying.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 27, 2012, 9:11pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/5 "2012-09-27T21:11:16Z")

</div>

Templating here isn't really a python thing.

You can just do:

${shared\_folder.stdout}

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 9:14pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/6 "2012-09-27T21:14:47Z")

</div>

Ohhh man that is simple!!!

and it works…

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 9:28pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/7 "2012-09-27T21:28:56Z")

</div>

and can I use {{ shared\_folder.stdout }} on a template file ?

I’m getting an error now for that.

or maybe reassign that value to another variable ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 27, 2012, 9:40pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/8 "2012-09-27T21:40:11Z")

</div>

In a template you need to either use the same syntax:

${shared\_folder.stdout}

OR

{{ shared\_folder['stdout'] }}

as Jinja2 is essentially python code inside the "{{" and "}}"

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 9:58pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/9 "2012-09-27T21:58:48Z")

</div>

I tried both no dice…

my template file looks like this:

config.vm.share\_folder “v-data”, “${shared\_folder.stdout}”, “${shared\_folder.stdout}”

and the task looks like this :

- name: Configuring template for Vagrant file  
action: template src=$local\_dir/ansible-vagrant/templates/Vagrantfile.j2 dest=$local\_dir/Vagrantfile

and the cat of the Vagranfile after creation :

config.vm.share\_folder “v-data”, “{u’changed’: True, u’end’: u’2012-09-27 14:57:49.037671’, u’stdout’: u’/Developer’, u’cmd’: [u’python’, u’directoryselection.py’], u’rc’: 0, u’start’: u’2012-09-27 14:57:46.636426’, u’stderr’: u’‘, u’delta’: u’0:00:02.401245’, ‘invocation’: {‘module\_name’: ‘command’, ‘module\_args’: u’python directoryselection.py’}}”, “{u’changed’: True, u’end’: u’2012-09-27 14:57:49.037671’, u’stdout’: u’/Developer’, u’cmd’: [u’python’, u’directoryselection.py’], u’rc’: 0, u’start’: u’2012-09-27 14:57:46.636426’, u’stderr’: u’‘, u’delta’: u’0:00:02.401245’, ‘invocation’: {‘module\_name’: ‘command’, ‘module\_args’: u’python directoryselection.py’}}”

if I use any other variable predefined my template works fine.

Thanks for all the help.

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 27, 2012, 11:34pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/10 "2012-09-27T23:34:06Z")

</div>

mmmmm doesn’t work and base on the jinja2 examples it is in the right format.

could be a bug or something like that ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 28, 2012, 2:00am UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/11 "2012-09-28T02:00:22Z")

</div>

this works fine:

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [September 28, 2012, 3:48am UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/12 "2012-09-28T03:48:07Z")

</div>

I just added :

gather\_facts: False

And it worked!!!

could you explain why ?

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [September 28, 2012, 8:31am UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/13 "2012-09-28T08:31:40Z")

</div>

> I just added :
> 
> gather\_facts: False
> 
> And it worked!!!
> 
> could you explain why ?

It works either way.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [October 15, 2012, 11:50pm UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/14 "2012-10-15T23:50:52Z")

</div>

Hi guys.

I have been trying to resolve this issue and for some reason I’m still getting the same error.

so I have this now:

- name: Configuring template for Vagrant file  
action: template src=$local\_dir/ansible-vagrant/templates/Vagrantfile.j2 dest=$local\_dir/Vagrantfile

- name: Creating nginx virtual host config for VM  
action: template src=$local\_dir/ansible-vagrant/templates/projectname.j2 dest=$local\_dir/ansible-vagrant/projectname\_vhost

The first file gets created just fine with the proper path in the template but the second one adds this to the vhost config :

root {u’changed’: True, u’end’: u’2012-10-15 16:21:52.672658’, u’stdout’: u’/Users/jamengual/Sites/projectname/meedan-projectname/drupal’, u’cmd’: [u’python’, u’directoryselection.py’], u’rc’: 0, u’start’: u’2012-10-15 16:21:41.577213’, u’stderr’: u’‘, u’delta’: u’0:00:11.095445’, ‘invocation’: {‘module\_name’: u’command’, ‘module\_args’: u’python directoryselection.py’}, ‘stdout\_lines’: [u’/Users/jamengual/Sites/projectname/meedan-projectname/drupal’]};

is only the second time that I use the same variable for another template file.

Any ideas ?

---

<div class="post-metadata">

**Author:** ![PePe\_Amengual](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@PePe\_Amengual](https://forum.ansible.com/u/PePe_Amengual)\
**Post date:** [October 16, 2012, 12:09am UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/15 "2012-10-16T00:09:09Z")

</div>

Please ignore it was a freaking TYPO!!!

Thanks to diff for that.

---

<div class="post-metadata">

**Author:** ![Nick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@Nick\_Doyle](https://forum.ansible.com/u/Nick_Doyle)\
**Post date:** [February 20, 2014, 1:03am UTC](https://forum.ansible.com/t/calling-external-program-and-get-output-into-a-variable/11935/16 "2014-02-20T01:03:27Z")

</div>

For future people, I just found a neat way to do this without having to add “.stdout” to your templates.

- name: get gridfs host ip from name (required for nginx gridfs module)  
shell: ‘ping -q -c 1 -t 1 {{ gridfs\_host }} | grep PING | sed -e “s/)._//" | sed -e "s/._(//”’  
register: gridfs\_ip\_command  
when: gridfs\_ip is not defined

- name: set gridfs ip  
set\_fact: gridfs\_ip={{ gridfs\_ip\_command.stdout }}  
when: gridfs\_ip is not defined
