Variable
Ansible - 訪問任務中的組變數
我的庫存文件配置了組變數。例子:
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 }}
不是正確的方法。
只需引用變數。例如劇本
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" }