Hi,
I’m trying to create a python script where I can specify the hosts within the script.
Something like:
webuptime = ansible.runner.Runner(
pattern=‘all’, forks=10,
module_name=‘command’, module_args=‘uptime’,
inventory = “webserver1,webserver2”
).run()
…or with a variable for the webserver bit. I can’t seem to figure it out though. Is there a simple way to do this without writing the names out to a file and reading them back in again? I can use an IP address instead if necessary.
Many thanks, Seumas
Why not use an executable inventory script? We include several examples of these in the ansible git repository (for example, the ec2.py script).
If you are doing inventory via the API you should use the Inventory class.
–Michael
Hi Michael,
Is there any documentation or examples of how to use that to do what I want?
Thanks, Seumas
Hi James,
I guess I could. I thought there must be an easy way of doing it directly though?
/usr/bin/ansible and /usr/bin/ansible-playbook both construct Inventory() objects.
Ok, it seems it’s not too complicated. You can do something like:
hosts = [“webserver1”,“webserver2”]
webInventory = ansible.inventory.Inventory(hosts)
webuptime = ansible.runner.Runner(
pattern=‘webserver*’, forks=10,
module_name=‘command’, module_args=‘uptime’,
inventory = webInventory
).run()
Hope this helps anyone looking for the same.
Cheers, Seumas
This is useful, but where do the host specific ansible_ssh_* values get passed? I’m not seeing how to do it in the dynamic inventory json style either.
They get passed as standard inventory variables
[webservers]
foo ansible_ssh_port=4000
or just
[webservers]
foo:4000
etc
In external inventory those variables are just like any other variable.