0

I need to use host_vars in my ansible file to install a package on remote host, i am trying to pull a variable by the name package from host_vars dir in which a file ansi2 reside which is the hostname of remote machine....

---
 - hosts: all
   become: yes
   ignore_errors: yes
   gather_facts: no
   tasks:

   - name: install vim
     yum: name={{ package }} state=present

Dir Structure:

[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"

Error:

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
2
  • Whats the content of the file?
    – heemayl
    Jun 13, 2017 at 3:34
  • @heemayl i have updated the question! Jun 13, 2017 at 3:42

2 Answers 2

1

In your case, I assume the top level of the directory should contain files and directories like so:

/etc
+-- ansible/
    +-- host_vars/
        +-- ansi2.example.com.yml
    +-- service.yml
3
  • nope even that did not work same error! Jun 13, 2017 at 8:56
  • This is an urgent issue ,, any help appreciated!!! Jun 13, 2017 at 16:12
  • the file in host_vars should be called as your host, in ansible inventory. According to the fatal log in the initial post, it would suggest that name should be ansi2.example.com -- not ansi2.
    – SYN
    Jul 8, 2017 at 0:26
0

What could be the problem is in your inventory, what is in it?

The name given to the .yml file should be the same in the hosts file.

/etc/ansible/ansi2/hosts

[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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .