Read variable stored in host file inside Ansible module developed in BASH

213 views
Skip to first unread message

Uri Miller

unread,
May 20, 2014, 8:10:14 AM5/20/14
to ansible...@googlegroups.com
Hi,

I have developed a module in BASH, and I am able to run in using external arguments given from command line:

[root@ansible ansible]# ansible ap0tv01ras02pbsw0 -m ansible_startBladeAgent -a "serverName=ap0tv01ras02pbsw0v00 platformVersion=1.0.9"
ap0tv01ras02pbsw0 | success >> {
    "changed": false, 
    "msg": "Server ap0tv01ras02pbsw0v00 already running"
}


I would like to start this module without specifying the arguments in the command line, so i have set inside /etc/ansible/hosts file those variables for that specific host:

hosts:
ap0tv01ras02pbsw0 serverName=ap0tv01ras02pbsw0v00 platformVersion=1.0.9

My question is - how do I access those variables from my script?

parsing the command line vars inside my BASH script is done by: 

eval $(sed -e "s/\s?\([^=]+\)\s?=\s?\(\x22\([^\x22]+\)\x22|\x27\([^\x27]+\)\x27|\(\S+\)\)\s?/\1='\2'/p" $1)

and works fine.


Thanks


Petros Moisiadis

unread,
May 20, 2014, 2:07:16 PM5/20/14
to ansible...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/50d9fe69-8286-4292-aada-09a437dba39f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ansible creates a file for you that holds the arguments passed to the module. The path to this file is passed as the first argument to your module script. So, your bash script must read and parse that file.

Michael DeHaan

unread,
May 21, 2014, 6:05:01 PM5/21/14
to ansible...@googlegroups.com
Writing modules in Bash is a great topic for ansible-devel.

FWIW, it's as easy as "source $1" inside your bash module, reading the key=value arguments into bash variables.

Then you just need to emit key=value pairs one per line, or JSON, as you choose from your bash module.

But yes, ansible-devel please!




Reply all
Reply to author
Forward
0 new messages