[英]How do I list hosts using Ansible 1.x API
Ansible-playbook has a --list-hosts
cli switch that just outputs the hosts affected by each play in a playbook. Ansible-playbook 有一个
--list-hosts
cli 开关,它只输出受 playbook 中每个 play 影响的主机。 I am looking for a way to access to same information through the python API.我正在寻找一种通过 python API 访问相同信息的方法。
The (very) basic script I am using to test right now is我现在用来测试的(非常)基本脚本是
#!/usr/bin/python
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
# hosts list
hosts = ["127.0.0.1"]
# set up the inventory, if no group is defined then 'all' group is used by 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 the hosts group
)
out = pm.run()
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))
I just can't figure out what to add to ansible.runner.Runner()
to make it output affected hosts and exit.我只是不知道要向
ansible.runner.Runner()
添加什么以使其输出受影响的主机并退出。
I'm not sure what are you trying to achieve, but ansible.runner.Runner
is actually one task and not playbook.我不确定你想实现什么,但
ansible.runner.Runner
实际上是一项任务而不是剧本。
Your script is a more kind of ansible CLI and not ansible-playbook .您的脚本是一种更可靠的CLI 而不是ansible-playbook 。
And ansible
doesn't have any kind of --list-hosts
, while ansible-playbook
does. ansible
没有任何类型的--list-hosts
,而ansible-playbook
有。
You can see how listhosts is done here .您可以在此处查看 listhosts 是如何完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.