如何在 Ansible 中循环 inventory_hostname
How to loop over inventory_hostname in Ansible
我正在尝试将动态清单的所有主机添加到组:可达。
这是我的剧本。
$ cat collect_try.yaml
---
- hosts: "{{ env }}:&{{ zone }}"
become: true
tasks:
- add_host:
name: "{{ inventory_hostname }}"
group: reachable
- name: dynamicgroup
hosts: reachable
gather_facts: false
tasks:
- debug: msg="{{ inventory_hostname }} is reachable"
这是我的输出:
TASK [Gathering Facts]
ok: [vm1.nodekite.com]
ok: [vm2.nodekite.com]
ok: [vm3.nodekite.com]
ok: [vm4.nodekite.com]
TASK [add_host]
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"reachable"
],
"host_name": "vm1.nodekite.com",
"host_vars": {
"group": "reachable"
}
},
"changed": true
}
PLAY [dynamicgroup]
META: ran handlers
TASK [debug]
ok: [vm1.nodekite.com] => {
"msg": "vm1.nodekite.com is reachable"
PLAY RECAP:
vm1.nodekite.com : ok=3 changed=1 unreachable=0 <=====
vm2.nodekite.com : ok=1 changed=0 unreachable=0
vm3.nodekite.com : ok=1 changed=0 unreachable=0
vm4.nodekite.com : ok=1 changed=0 unreachable=0
如何使用循环将所有主机添加到“group”:“reachable”。有人可以帮忙吗。
来自add_host
documentation notes:
This module bypasses the play host loop and only runs once for all the hosts in the play, if you need it to iterate use a with-loop construct.
在您的特定情况下(即您游戏中的动态主机模式),您应该能够使用 inventory_hostnames
lookup 实现您的要求,例如(未完全测试):
- name: Collect reachable hosts
hosts: localhost
gather_facts: false
tasks:
- name: Push hosts to "reachable" group
vars:
pattern: "{{ env }}:&{{ zone }}"
add_host:
name: "{{ item }}"
group: reachable
loop: "{{ query('inventory_hostnames', pattern) }}"
我正在尝试将动态清单的所有主机添加到组:可达。
这是我的剧本。
$ cat collect_try.yaml
---
- hosts: "{{ env }}:&{{ zone }}"
become: true
tasks:
- add_host:
name: "{{ inventory_hostname }}"
group: reachable
- name: dynamicgroup
hosts: reachable
gather_facts: false
tasks:
- debug: msg="{{ inventory_hostname }} is reachable"
这是我的输出:
TASK [Gathering Facts]
ok: [vm1.nodekite.com]
ok: [vm2.nodekite.com]
ok: [vm3.nodekite.com]
ok: [vm4.nodekite.com]
TASK [add_host]
changed: [vm1.nodekite.com] => {
"add_host": {
"groups": [
"reachable"
],
"host_name": "vm1.nodekite.com",
"host_vars": {
"group": "reachable"
}
},
"changed": true
}
PLAY [dynamicgroup]
META: ran handlers
TASK [debug]
ok: [vm1.nodekite.com] => {
"msg": "vm1.nodekite.com is reachable"
PLAY RECAP:
vm1.nodekite.com : ok=3 changed=1 unreachable=0 <=====
vm2.nodekite.com : ok=1 changed=0 unreachable=0
vm3.nodekite.com : ok=1 changed=0 unreachable=0
vm4.nodekite.com : ok=1 changed=0 unreachable=0
如何使用循环将所有主机添加到“group”:“reachable”。有人可以帮忙吗。
来自add_host
documentation notes:
This module bypasses the play host loop and only runs once for all the hosts in the play, if you need it to iterate use a with-loop construct.
在您的特定情况下(即您游戏中的动态主机模式),您应该能够使用 inventory_hostnames
lookup 实现您的要求,例如(未完全测试):
- name: Collect reachable hosts
hosts: localhost
gather_facts: false
tasks:
- name: Push hosts to "reachable" group
vars:
pattern: "{{ env }}:&{{ zone }}"
add_host:
name: "{{ item }}"
group: reachable
loop: "{{ query('inventory_hostnames', pattern) }}"