Creating proper (constructed) inventory

Hi guys,

I want to start and say that I’m really new to Ansible. Lately I’ve started working with inventories to manage my hosts on different platforms (on-prem, cloud etc.). I’ve explored group_vars and host_vars and all of that works fine in a very very simple setup, where I define one or two groups and include these hosts. But I want more :).

The thing I’m struggling with (and maybe I’m not aware of the limitations yet, or it’s just my lack of knowledge at this point) is that I’d like to set up an inventory in AAP based on the following inventory file. Basically I want to be able to distinguish ‘hana’ systems and ‘netweaver’ systems, in different cloud regions and either nonprod or prod.

---
all:
  azure:
    children:
      weu:
        children:
          hana:
            children:
              nonprod:
                hosts:
                  s10000001.customer.com:
                  s10000002.customer.com:
              prod:
                hosts:
                  s11000001.customer.com:
          netweaver:
            children:
              nonprod:
                hosts:
                  s20000001.customer.com:
                  s20000002.customer.com:        
      frc:
        children:
          hana:
            children:
              nonprod:
                hosts:
                  s30000001.customer.com:
                  s30000002.customer.com:

If I run this file through the inventory sync on AAP it doesn’t show any hosts or groups as a result. Clearly I’m doing something wrong and I probably can’t grasp the full concept yet, but I’m hoping you guys can help me find a solution to make this work.

Thanks,
Niels

I think this might be wrong:

I’d probably use a flatter structure, something like this? :woman_shrugging:

all:
  children:
    azure:
      hosts:
        s10000001.customer.com:
        s10000002.customer.com:
        s11000001.customer.com:
        s20000001.customer.com:
        s20000002.customer.com:
        s30000001.customer.com:
        s30000002.customer.com:
    hana:
      hosts:
        s10000001.customer.com:
        s10000002.customer.com:
        s11000001.customer.com:
    nonprod:
       hosts:
         s10000001.customer.com:
         s10000002.customer.com:

Looking at the inventory documentation for YAML inventories, it looks like you need to define your hosts, and then define your groups. Specifically, using the children: directive, and then also defining hosts there doesn’t appear to align with the example. While I haven’t tested this, I think you’d do something more like this:

azure:
  children:
    weu:
    frc:
    			  
nonprod:
  hosts:
    s30000001.customer.com:
    s30000002.customer.com:
    s10000001.customer.com:
    s10000002.customer.com:
    s20000001.customer.com:
    s20000002.customer.com:
prod:
  hosts:
    s11000001.customer.com:
hana:
  hosts:
    s30000001.customer.com:
    s30000002.customer.com:
    s10000001.customer.com:
    s10000002.customer.com:
    s11000001.customer.com:
netweaver:
  hosts:
    s20000001.customer.com:
    s20000002.customer.com:
weu:
  hosts:
    s10000001.customer.com:
    s10000002.customer.com:
    s20000001.customer.com:
    s20000002.customer.com:
frc:
  hosts:
    s30000001.customer.com:
    s30000002.customer.com:

And then your playbook would target hana&prod for production hana systems (based on the common patterns documentation).

Hope that helps.

1 Like

Thanks very much for clarifying this. I tested Chris’ solution and that works nicely. I still don’t like the idea that I have to add a host to multiple individual groups, but I guess that kind of setup just doesn’t work.

1 Like