[英]Ansible: How to use variables defined in inventory file (hosts) in my playbook?
[英]Ansible: Use variables in a playbook defined in different file (not the inventory file)
我想使用在我的劇本的文件( hosts_vars
)中定義的變量。 這是包含劇本的目錄樹:
├── ansible.cfg
├── ansible.log
├── choco_deploy.yml
├── group_vars
│ └── win_clones.yml
├── hosts
├── hosts_vars
我的主機文件:
[win_clones]
cl1 ansible_host='IP1'
cl3 ansible_host='IP2'
hosts_vars
文件如下所示:
---
cl1:
to_install:
- soft1
- soft2 46.0.1
to_uninstall:
- soft3 2.2.3
cl3:
to_install:
- soft4 2.2.3
- soft5 6.9
- sof6 6.9
to_uninstall:
- soft77
在我的劇本中,我想參考例如host_vars/cl1/to_install/soft1
或遍歷host_vars/cl3/to_install/
所有變量。 就像是:
tasks:
- name: Installing the packages ...
win_chocolatey:
name: "{{ package_id }}" # here to pass variables defined in host_vars
state: present
我怎樣才能做到這一點? 謝謝
在您的劇本中,您可以添加vars_files
來指定外部變量文件,如下所示:
---
hosts: all
remote_user: root
vars_files:
- path/to/hosts_vars
tasks:
- name: my awesome task
command: /bin/echo {{ myvar }}
在您的hosts_vars
:
---
myvar: "foo"
myothervar: "bar"
請參閱: 有關變量文件分離的文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.