Search code examples
ansibleansible-2.xansible-inventory

Ansible group vars not getting loaded


Group vars are not getting imported. The directory structure looks like this:

 - ansible.cfg
 - testt.yaml
 - inventories
     - production 
          - group_vars
                - all.yaml
          - host_vars
          - hosts
     - staging
          - group_vars
                - all.yaml
                - check99.yaml
          - host_vars
          - hosts 

ansible.cfg looks like this:

     inventory= inventories
    

staging/hosts looks like:

     target99 ansible_connection=local
    
     [check99]
     target99
    

staging/group_vars/check99.yaml looks like:

      hello: abc

testt.yaml looks like:

    - hosts: target99
      tasks: 
         - debug:
             msg: >
                {{hello}} 

When I run command: ansible-playbook testt.yaml, I get error variable hello undefined, what is wrong here?


Solution

  • You are setting the inventory to an incorrect directory

    Try setting the inventory in ansible.cfg

    inventory=inventories/staging/