Ansible playbook有一个--list-hosts
cli开关,它只输出playbook中受每个播放影响的主机。我正在寻找一种通过python API访问相同信息的方法。
我现在用来测试的(非常)基本的脚本是
#!/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=(',', ': '))
我只是不知道该向ansible.runner.Runner()
添加什么,使其输出受影响的主机并退出。
我不确定你想实现什么,但ansible.runner.Runner
实际上是一项任务,而不是剧本
您的脚本是一种更易回答的CLI,而不是易回答的剧本
并且ansible
没有任何类型的--list-hosts
,而ansible-playbook
有
你可以在这里看到列表主机是如何完成的。