Ken Chen
Ken Chen

Reputation: 62

Ansible is not picking the group_vars and host_vars

I am new to Ansible and couldn't figure out why the playbook is not picking up the group_vars/ and host_vars I have defined. According to the document:

You can also add group_vars/ and host_vars/ directories to your playbook directory. The ansible-playbook command looks for these directories in the current working directory by default.

My playbook, inventory, and other files structure are quite simple. It should be matching the default.

enter image description here

Inventory file:

dummy

[spider]
s0ra
s0ra_slave

The playbook:

- name: base mix release upgrade Prod.
  hosts: spider
  gather_facts: false

  # vars_files:
  # - vars/s0ra_sup.yaml

  tasks:
    - name: check release bin
      stat:
        path: "{{ sh_lastrel }}"
      register: rel_bin

When I tried to run the playbook by ansible-playbook -i inventory.ini mix_upgrade.yaml, it complains:

PLAY [base mix release upgrade Prod.] **********************************************************************************

TASK [check release bin] ***********************************************************************************************
fatal: [s0ra]: FAILED! => {"msg": "The task includes an option with an undefined variable. 
The error was: 'sh_lastrel' is undefined\n\n
The error appears to be in 'xxx/ansible/mix_upgrade.yaml': line 19, column 7, but may\n
be elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n\n
    - name: check release bin\n      ^ here\n"}

PLAY RECAP *************************************************************************************************************
s0ra                       : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

But the sh_lastrel is defined in spider.yaml actually. I don't know why it is not loaded. I tried to turn on -v mode but it does not seem to have more debugging info. Any hint of the cause or how to further debug is greatly appreciated.

My ansible version is as below:

╰─ ansible --version                                                                                   ✔  22:01:57 
ansible 2.9.9
  config file = None
  configured module search path = ['/Users/kenchen/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kenchen/.pyenv/versions/3.8.2/lib/python3.8/site-packages/ansible
  executable location = /Users/kenchen/.pyenv/versions/3.8.2/bin/ansible
  python version = 3.8.2 (default, May 18 2020, 00:02:00) [Clang 10.0.1 (clang-1001.0.46.4)]

Upvotes: 0

Views: 3194

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68189

Make sure group_vars/spider.yml is available. For example,

shell> cat group_vars/spider.yml 
sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml
shell> cat inventory.ini
dummy
[spider]
s0ra
s0ra_slave
shell> cat mix_upgrade.yaml 
- hosts: spider
  gather_facts: false
  tasks:
    - debug:
        var: sh_lastrel
shell> ansible-playbook -i inventory.ini mix_upgrade.yaml 

ok: [s0ra] => 
  sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml
ok: [s0ra_slave] => 
  sh_lastrel: value of sh_lastrel defined in group_vars/spider.yml

Upvotes: 0

Related Questions