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

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

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.

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!