Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible nested group_vars (use only if member in group A and B)

My Inventory is nested like so:

customerA:
  children:
    webserver:
      hosts:
        host1:
        host2:
    dbserver:
      hosts:
        host3:
        host4:
customerB:
  children:
    webserver:
      hosts:
        host5:
        host6:
    dbserver:
      hosts:
        host7:
        host8:

So for host1 ansible loads:

host_vars/host1
group_vars/customerA
group_vars/webserver

Is there a way to create a nested_vars/customerA/webserver.yml (same as nested_vars/webserver/customerA.yml) structure that gets used by the inventory management?

Or do I have to use something like

group_vars/customerA
- parent: "customerA"
  
group_vars/webserver:
- child: "webserver"

in every playbook:

include_vars:
  file: "{{ parent }}+{{ child }}.yml"

or create new groups for all combinations I need to target?

like image 884
Jochen Avatar asked Oct 14 '25 11:10

Jochen


1 Answers

This is built upon this other answer


Create your base static inventory

inventories/default/0-hosts.yml

---
customerA:
  children:
    customerA_webservers:
      hosts:
        host1:
        host2:
    customerA_dbservers:
      hosts:
        host3:
        host4:
customerB:
  children:
    customerB_webservers:
      hosts:
        host5:
        host6:
    customerB_dbservers:
      hosts:
        host7:
        host8:

Make your type groups (e.g. webservers, dbservers) dynamic using the above naming convention with the constructed inventory plugin

inventories/default/1-typegroups_constructed.yml

---
plugin: constructed
groups:
  dbservers: group_names | select('match', '^.*_dbservers$') | length > 0
  webservers: group_names | select('match', '^.*_webservers$') | length > 0

You now have a complete fine grained group membership:

$ ansible-inventory -i inventories/default/ --list
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped",
            "customerA",
            "customerB",
            "webservers",
            "dbservers"
        ]
    },
    "customerA": {
        "children": [
            "customerA_webservers",
            "customerA_dbservers"
        ]
    },
    "customerA_dbservers": {
        "hosts": [
            "host3",
            "host4"
        ]
    },
    "customerA_webservers": {
        "hosts": [
            "host1",
            "host2"
        ]
    },
    "customerB": {
        "children": [
            "customerB_webservers",
            "customerB_dbservers"
        ]
    },
    "customerB_dbservers": {
        "hosts": [
            "host7",
            "host8"
        ]
    },
    "customerB_webservers": {
        "hosts": [
            "host5",
            "host6"
        ]
    },
    "dbservers": {
        "hosts": [
            "host3",
            "host4",
            "host7",
            "host8"
        ]
    },
    "webservers": {
        "hosts": [
            "host1",
            "host2",
            "host5",
            "host6"
        ]
    }
}

Which makes you able to address:

  • variables for a customer in e.g.:
    inventories/default/group_vars/customerA.yml
    
  • variables for a server type in e.g.:
    inventories/default/group_vars/dbservers.yml
    
  • variables for a server type for one customer in e.g.:
    inventories/default/group_vars/customerB_webservers.yml
    
like image 102
Zeitounator Avatar answered Oct 18 '25 02:10

Zeitounator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!