我需要host_vars
在我的ansible文件中使用在远程主机上安装一个包,我试图 package
从host_vars
文件所在的目录中按名称拉取一个变量ansi2
,该文件是远程计算机的主机名....
---
- hosts: all
become: yes
ignore_errors: yes
gather_facts: no
tasks:
- name: install vim
yum: name={{ package }} state=present
目录结构:
[root@ansi1 ansible]# pwd
/etc/ansible
[root@ansi1 ansible]#
[root@ansi1 ansible]# ls -l ansi2/host_vars/
total 4
-rw-r--r-- 1 root root 19 Jun 13 08:15 ansi2
[root@ansi1 ansible]#
[root@ansi1 ansible]# cat ansi2/host_vars/ansi2
---
package: "vim"
错误:
TASK [install vim] *************************************************************
task path: /etc/ansible/service.yml:7
fatal: [ansi2.example.com]: FAILED! => {
"failed": true,
"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'package' is undefined\n\nThe error appears to have been in '/etc/ansible/service.yml': line 7, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: install vim\n ^ here\n"
}
...ignoring
答案1
在您的情况下,我假设目录的顶层应包含如下文件和目录:
/etc
+-- ansible/
+-- host_vars/
+-- ansi2.example.com.yml
+-- service.yml
答案2
您的库存可能存在什么问题,里面有什么?
.yml 文件的名称应与主机文件中的名称相同。
/etc/ansible/ansi2/主机
[webdb]
127.0.0.1 ansible_user=root
/etc/ansible/ansi2/host_vars/webdb.yml
---
some_var: "temp"
/etc/ansible/service.yml
---
- hosts: all
become: yes
ignore_errors: yes
gather_facts: no
tasks:
- name: install vim
yum: name={{ package }} state=present