Ansible 嵌套循环遍历清单
Ansible nested loops through inventories
我正在查看 Ansible 的循环页面 https://docs.ansible.com/ansible/2.7/user_guide/playbooks_loops.html 但我找不到我要找的东西...
我想测试 "source" 主机是否可以到达/连接到一组端口上的一组目标主机。例如,每组目标主机可以是一个清单组,每个清单组都有自己要测试的端口列表。
我们的想法是仅指定两个变量作为剧本的输入:一组主机列表和一个或多个源主机列表。
循环应该怎么做?清单可以是 YAML 或 INI 文件:每个组一个或多个。 TCP 连接的测试很简单...所以重点只放在输入变量和循环构造上。
感谢和问候。
史蒂夫
给定以下 hosts
库存文件
server1
server2
server3
[dev]
d1
d2
d3
[dev:vars]
ports=8080,8443
[int]
i1
i2
i3
[int:vars]
ports=80,443
和以下剧本play.yml
---
- hosts: all
gather_facts: False
vars:
group: 'dev'
tasks:
- name: Just loop through a group and group_vars
debug:
msg: 'group is {{group}} target is {{item.0}} port is {{item.1}}'
loop: >
{{ groups[group] |
product(hostvars[groups[group][0]]["ports"]) |
list }}
您可以按照您的描述遍历库存
ansible-playbook -i hosts play.yml -l server* -e group=int
产生以下结果:
PLAY [all] *********************************************************************
TASK [Just loop through a group and group_vars] ********************************
ok: [server1] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server1] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server2] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server2] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server1] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server3] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server1] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server3] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server2] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server1] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server3] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server2] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server1] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
ok: [server3] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server2] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server3] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server2] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
ok: [server3] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
PLAY RECAP *********************************************************************
server1 : ok=1 changed=0 unreachable=0 failed=0
server2 : ok=1 changed=0 unreachable=0 failed=0
server3 : ok=1 changed=0 unreachable=0 failed=0
例如,您只需将调试替换为 wait_for module。
我正在查看 Ansible 的循环页面 https://docs.ansible.com/ansible/2.7/user_guide/playbooks_loops.html 但我找不到我要找的东西...
我想测试 "source" 主机是否可以到达/连接到一组端口上的一组目标主机。例如,每组目标主机可以是一个清单组,每个清单组都有自己要测试的端口列表。
我们的想法是仅指定两个变量作为剧本的输入:一组主机列表和一个或多个源主机列表。
循环应该怎么做?清单可以是 YAML 或 INI 文件:每个组一个或多个。 TCP 连接的测试很简单...所以重点只放在输入变量和循环构造上。
感谢和问候。
史蒂夫
给定以下 hosts
库存文件
server1
server2
server3
[dev]
d1
d2
d3
[dev:vars]
ports=8080,8443
[int]
i1
i2
i3
[int:vars]
ports=80,443
和以下剧本play.yml
---
- hosts: all
gather_facts: False
vars:
group: 'dev'
tasks:
- name: Just loop through a group and group_vars
debug:
msg: 'group is {{group}} target is {{item.0}} port is {{item.1}}'
loop: >
{{ groups[group] |
product(hostvars[groups[group][0]]["ports"]) |
list }}
您可以按照您的描述遍历库存
ansible-playbook -i hosts play.yml -l server* -e group=int
产生以下结果:
PLAY [all] *********************************************************************
TASK [Just loop through a group and group_vars] ********************************
ok: [server1] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server1] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server2] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server2] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server1] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server3] => (item=[u'i1', 80]) => {
"msg": "group is int target is i1 port is 80"
}
ok: [server1] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server3] => (item=[u'i1', 443]) => {
"msg": "group is int target is i1 port is 443"
}
ok: [server2] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server1] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server3] => (item=[u'i2', 80]) => {
"msg": "group is int target is i2 port is 80"
}
ok: [server2] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server1] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
ok: [server3] => (item=[u'i2', 443]) => {
"msg": "group is int target is i2 port is 443"
}
ok: [server2] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server3] => (item=[u'i3', 80]) => {
"msg": "group is int target is i3 port is 80"
}
ok: [server2] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
ok: [server3] => (item=[u'i3', 443]) => {
"msg": "group is int target is i3 port is 443"
}
PLAY RECAP *********************************************************************
server1 : ok=1 changed=0 unreachable=0 failed=0
server2 : ok=1 changed=0 unreachable=0 failed=0
server3 : ok=1 changed=0 unreachable=0 failed=0
例如,您只需将调试替换为 wait_for module。