Ansible - 在运行时使用当前主机动态访问主机变量
Ansible - dynamically access hostvars with current hosts in play during runtime
使用 Ansible 2.5.0.
问题:如何在运行时动态访问当前主机的主机变量。
库存文件:
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3 ansible_host=2.4.6.8
我当前的任务:
- hosts: group_one
tasks:
- name: set fact for all ip's in play execution
set_fact:
all_ips: "{{ groups['all'] | map('extract', hostvars, ['ansible_host'] ) | join(',') }}"
# outcome = 1.2.3.4,5.6.7.8,2.4.6.8
这种结果是不希望的。执行时只使用了host-1和2,但同时添加了host-3的IP。
现在,我查看了 Ansible 的 magic variables,但似乎没有可以满足我需要的方式使用的变量。
因为我在运行时需要变量,所以我不想设置变量 "{{ groups['group_one'] }}"
,因为这会破坏目的。
如何在不静态配置任何变量的情况下以这种方式配置 Ansible,结果会是 1.2.3.4,5.6.7.8
?
如果不想使用 group_one
.
,可以使用 ansible_play_hosts
magic variable 作为当前主机的动态引用
ansible_play_hosts
is the full list of all hosts still active in the current play.
inventory.ini
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3 ansible_host=2.4.6.8
foo.yaml
- hosts: group_one
gather_facts: no
tasks:
- set_fact:
all_ips: "{{ ansible_play_hosts | map('extract', hostvars, ['ansible_host']) | join(',') }}"
- debug:
msg: "{{ all_ips }}"
run_once: yes
$ ansible-playbook -i inventory.ini foo.yaml
PLAY [group_one] *****************************************************************************************************************************************************************************************************************************
TASK [set_fact] ******************************************************************************************************************************************************************************************************************************
ok: [host-1]
ok: [host-2]
TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [host-1] => {
"msg": "1.2.3.4,5.6.7.8"
}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
host-1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host-2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
使用 Ansible 2.5.0.
问题:如何在运行时动态访问当前主机的主机变量。
库存文件:
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3 ansible_host=2.4.6.8
我当前的任务:
- hosts: group_one
tasks:
- name: set fact for all ip's in play execution
set_fact:
all_ips: "{{ groups['all'] | map('extract', hostvars, ['ansible_host'] ) | join(',') }}"
# outcome = 1.2.3.4,5.6.7.8,2.4.6.8
这种结果是不希望的。执行时只使用了host-1和2,但同时添加了host-3的IP。
现在,我查看了 Ansible 的 magic variables,但似乎没有可以满足我需要的方式使用的变量。
因为我在运行时需要变量,所以我不想设置变量 "{{ groups['group_one'] }}"
,因为这会破坏目的。
如何在不静态配置任何变量的情况下以这种方式配置 Ansible,结果会是 1.2.3.4,5.6.7.8
?
如果不想使用 group_one
.
ansible_play_hosts
magic variable 作为当前主机的动态引用
ansible_play_hosts
is the full list of all hosts still active in the current play.
inventory.ini
[group_one]
host-1 ansible_host=1.2.3.4
host-2 ansible_host=5.6.7.8
[group_two]
host-3 ansible_host=2.4.6.8
foo.yaml
- hosts: group_one
gather_facts: no
tasks:
- set_fact:
all_ips: "{{ ansible_play_hosts | map('extract', hostvars, ['ansible_host']) | join(',') }}"
- debug:
msg: "{{ all_ips }}"
run_once: yes
$ ansible-playbook -i inventory.ini foo.yaml
PLAY [group_one] *****************************************************************************************************************************************************************************************************************************
TASK [set_fact] ******************************************************************************************************************************************************************************************************************************
ok: [host-1]
ok: [host-2]
TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [host-1] => {
"msg": "1.2.3.4,5.6.7.8"
}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
host-1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
host-2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0