我的库存文件配置了组变量。例子:
all:
children:
europe:
vars:
network_id: 3
network_name: "europe-eu"
hosts:
europe-eu-1254:
ansible_host: 8.8.8.8
ansible_ssh_pass: password
ansible_ssh_user: user
...
我想在我的任务中获取组变量,但我不知道如何。
任务示例:
- name: Start latest container
docker_container:
name: "server-{{ hostvars[inventory_hostname].vars.network_name }}"
image: "{{ docker_registry }}:{{ docker_tag }}"
state: started
recreate: yes
network_mode: host
oom_killer: no
restart_policy: always
become: yes
...
我认为,这{{ hostvars[inventory_hostname].vars.network_name }}
不是正确的方法。
答案1
只需引用变量即可。例如剧本
shell> cat playbook.yml
- hosts: all
tasks:
- debug:
var: network_name
- debug:
msg: "{{ network_name }}"
给出(删节)
shell> ansible-playbook playbook.yml
ok: [europe-eu-1254] => {
"network_name": "europe-eu"
}
ok: [europe-eu-1254] => {
"msg": "europe-eu"
}