Referencing Ansible group variables to inventory

I have an inventory file inv.yml with a list of hosts which looks like this:

---
all:
  hosts:
    csr1:
      ansible_host: 192.168.10.11
      os: ios
    csr2:
      # etc...

I have a group_vars folder containing a few files. One of them is all.yml and I can see that all hosts are getting variables from this file. I also have two other files amers.yml and emear.yml. Each contains a specific snmp config per region which looks like that:

---
snmp:
  contact: Joe Smith
  location: Americas
  communities:
    - community: public
      type: ro

My problem is that I don't know how to reference which host should use the emear or amers group variable file.

How can I correctly reference amers and emear variable files for specific devices in inventory?


Your inventory should look like (example, put the correct hosts in each group)

---
all:
  children:
    amers:
      hosts:
        csr1:
          ansible_host: 1.2.3.4 
    emear:
      hosts:
        csr2:
          ansible_host: 5.6.7.8

Now each group will receive the variables defined in the respective group_vars/amers.yml and group_vars/emear.yml files.

This is a very short intro, there is a lot more to know about how to construct your inventory, where to put your variables exactly (to skip possible variables precedence nightmares...), good practice, etc. You can find all this in the ansible documentation: How to build your inventory


I'm not sure your groups are defined. Try creating a hosts.ini (see below) and passing it to ansible-playbook

$ ansible-inventory -i hosts.ini --list

Files:

├── group_vars
│   ├── all.yml
│   ├── amers.yml
│   └── emers.yml
└── hosts.ini

hosts.ini

[amers]
192.168.10.11

[emers]
192.168.10.12

group_vars/amers.yml

---
snmp:
  contact: Joe Smith
  location: Americas
  communities:
    - community: public
      type: ro

group_vars/emers.yml

---
snmp:
  contact: Nobody

group_vars/all.yml

---
os: ios