我有多台服务器,并且每台服务器上的清单中都有多个容器。一些服务器将在一个组中,另一些服务器在另一组中,我想为每个容器url变量指定。 我的库存:
我可以像with_dict一样遍历这些URL吗?如果可以,怎么办?
我的库存:
all:
hosts:
<1st-server-ip>:
ansible_host: <IP>
<2nd-server-ip>:
ansible_host: <IP>
<3rd-server-ip>:
ansible_host: <IP>
children:
<1st-GROUP>:
hosts:
<1st-server-ip>:
<containers>:
<app-1>:
url: '<URL>'
<app-2>:
url: '<URL>'
<app-3>:
url: '<URL>'
<2nd-GROUP>:
hosts:
<2nd-server-ip>:
<containers>:
<app-4>:
url: '<URL>'
<app-5>:
url: '<URL>'
<3rd-GROUP>:
hosts:
<3rd-server-ip>:
<containers>:
<app-6>:
url: '<URL>'
<app-7>:
url: '<URL>'
<app-8>:
url: '<URL>'
答案 0 :(得分:0)
json_query可以将结构简化为可以用include_tasks进行迭代的列表列表。
下面的戏
- include_tasks: loop-url.yml
loop: "{{ all.children|json_query('*.hosts.*.containers[]') }}"
loop_control:
loop_var: outer_item
包含任务
> cat loop-url.yml
- debug:
msg: "key: {{ item.key }} value: {{ item.value.url }}"
loop: "{{ outer_item|dict2items }}"
给予(grep msg):
"msg": "key: app-7 value: URL"
"msg": "key: app-6 value: URL"
"msg": "key: app-8 value: URL"
"msg": "key: app-5 value: URL"
"msg": "key: app-4 value: URL"
"msg": "key: app-3 value: URL"
"msg": "key: app-2 value: URL"
"msg": "key: app-1 value: URL"
(从数据中删除了标记符<>)