python - How do I list hosts using Ansible 1.x API -


ansible-playbook has --list-hosts cli switch outputs hosts affected each play in playbook. looking way access same information through python api.

the (very) basic script using test right is

#!/usr/bin/python  import ansible.runner import ansible.playbook import ansible.inventory ansible import callbacks ansible import utils import json  # hosts list hosts = ["127.0.0.1"] # set inventory, if no group defined 'all' group used default example_inventory = ansible.inventory.inventory(hosts)  pm = ansible.runner.runner(     module_name = 'command',     module_args = 'uname -a',     timeout = 5,     inventory = example_inventory,     subset = 'all' # name of hosts group      )  out = pm.run()  print json.dumps(out, sort_keys=true, indent=4, separators=(',', ': ')) 

i can't figure out add ansible.runner.runner() make output affected hosts , exit.

i'm not sure trying achieve, ansible.runner.runner 1 task , not playbook.
script more kind of ansible cli , not ansible-playbook.
, ansible doesn't have kind of --list-hosts, while ansible-playbook does.
can see how listhosts done here.


Comments