calling external program and get output into a variable ?

Hi.

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

Thanks.

Absolutely.

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

- action: command /usr/bin/foo
  register: output_var

Now you can use ${output_var.stdout} and ${output_var.rc} etc

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.

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.

Templating here isn't really a python thing.

You can just do:

${shared_folder.stdout}

Ohhh man that is simple!!!

and it works…

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.

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 "}}"

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.

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

could be a bug or something like that ?

this works fine:

I just added :

gather_facts: False

And it worked!!!

could you explain why ?

I just added :

gather_facts: False

And it worked!!!

could you explain why ?

It works either way.

        -JP

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 ?

Please ignore it was a freaking TYPO!!!

Thanks to diff for that.

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