Search code examples
ansibleansible-inventory

Anible vars in inventories directory no applying


I am using a role (zaxos.lvm-ansible-role) to manage lvms on a few hosts. Initially I had my vars for the lvm under host_vars/server.yaml which works.

Here is the working layout

├── filter_plugins
├── group_vars
├── host_vars
│   ├── server1.yaml
│   └── server2.yaml
├── inventories
│   ├── preprod
│   ├── preprod.yml
│   ├── production
│   │   ├── group_vars
│   │   └── host_vars
│   ├── production.yaml
│   ├── staging
│   │   ├── group_vars
│   │   └── host_vars
│   └── staging.yml
├── library
├── main.yaml
├── module_utils
└── roles
    └── zaxos.lvm-ansible-role
        ├── defaults
        │   └── main.yml
        ├── handlers
        │   └── main.yml
        ├── LICENSE
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── create-lvm.yml
        │   ├── main.yml
        │   ├── mount-lvm.yml
        │   ├── remove-lvm.yml
        │   └── unmount-lvm.yml
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

For my environment it would make more sense to have the host_vars under the inventories directory which is also supported (Alternative Directory Layout) as per Ansible doc.

However when I change to this layout the vars are not initialized and the lvms on the host don’t change.

 ├── filter_plugins
├── inventories
│   ├── preprod
│   │   ├── group_vars
│   │   └── host_vars
│   │       ├── server1.yaml
│   │       └── server2.yaml
│   ├── preprod.yml
│   ├── production
│   │   ├── group_vars
│   │   └── host_vars
│   ├── production.yaml
│   ├── staging
│   │   ├── group_vars
│   │   └── host_vars
│   └── staging.yml
├── library
├── main.yaml
├── module_utils
└── roles
    └── zaxos.lvm-ansible-role
        ├── defaults
        │   └── main.yml
        ├── handlers
        │   └── main.yml
        ├── LICENSE
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── create-lvm.yml
        │   ├── main.yml
        │   ├── mount-lvm.yml
        │   ├── remove-lvm.yml
        │   └── unmount-lvm.yml
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

Any idea why this approach is not working?


Solution

  • Your host_vars directory must reside in ansible's discovered inventory_dir.

    With the above filetree, I guess you are launching your playbook with ansible-playbook -i inventories/preprod.yml yourplaybook.yml. In this context, ansible discovers inventory_dir as inventories

    The solution is to move your inventory files inside each directory for your environment, e.g. for preprod => mv inventories/preprod.yml inventories/preprod/

    You can then launch your playbook with ansible-playbook -i inventories/preprod/preprod.yml yourplaybook.yml and it should work as you expect.