您可以使用add_host
模块动态创建库存。
像这样的东西开始,并修改您的需要:
---
- hosts: localhost
tasks:
- add_host: name={{item}} group=hosts_from_webservice
with_url: https://mywebservice/host_list_as_simple_strings
# in this example web service should return one ip/hostname by line:
# 10.1.1.1
# 10.1.1.2
# 10.1.1.3
- add_host: name={{(item | from_json).host}} group=hosts_from_webservice description={{(item | from_json).desc}}
with_url: https://mywebservice/host_list_as_json_strings
# in this example web service should return JSON object on every line:
# {"host":"10.1.1.1","desc":"hello"}
# {"host":"10.1.1.2","desc":"world"}
# {"host":"10.1.1.3","desc":"test"}
- hosts: hosts_from_webservice
tasks:
- debug: msg="I'm a host from webservice"
谢谢。您是否知道预计从Web服务接收到的数据结构是什么?关于'with_url'的唯一文档位于[“更多查找”](http://docs.ansible.com/ansible/playbooks_lookups.html#more-lookups)中,并且仅表明该命令存在。我期望它是某种列表,以便'{{item}}'可以迭代它,但不明白究竟应该提供什么(它不是JSON形式的动态库存定义,它看起来应该是某种东西就像我提到的那样,我认为这是可迭代的)。 – WoJ
具体而言,我收到以下错误:“'通过'add_host'创建主机:hostname = {'hosts':['10 .11.2.16','10 .11.2.4','10 .11.2.5','10 .11.2.15', '10 .11.2.6']} 意外的异常:不可互换的类型:'dict' '我尝试了多个数据结构('hostname ='后可见的结构):列表,字典等 - 错误是他们都是(我希望在那一点上''hostname ='应该有一个主机名传递,通过Web服务的响应迭代 - 我不知道如何构造。 – WoJ
@WoJ我用例子更新了我的答案。'with_url '在http响应中迭代线 - 它是通用查找插件,可以用于任何任务,而不仅仅用于add_host。 –