我想在我的剧本中使用文件(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
我该怎么做?感谢
答案 0 :(得分:2)
在您的剧本中,您可以添加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"