Real World Example - nsg/ansible-inventory GitHub Wiki

This is a simplified small piece of a real inventory used in production, I have changed the names to keep things a little anonymous.

$ tree
.
├── hosts
│   ├── group_vars
│   │    ├── db.yml
│   │    └── stage.yml
│   ├── hosts_vars
│   │    └── stoprod-db01.yml
│   ├── inventory
│   │    ├── db.yml
│   │    └── www.yml
│   ├── inventory.yml
│   └── inventory.py
└── site.yml

inventory.yml

---
matcher:
  - regexp: "(sto|lon)(stage|prod|support)-.*"
    capture: True
company:
  vars:
    graphite_hostname: 10.24.90.1
  include:
    - inventory/db.yml
    - inventory/www.yml

We have plenty of hosts named in the pattern [site][env]-function01 and so on, the inventory.py will already split and make tag-groups for siteenv and function. siteenv is a useless name so I told the system to split it up under matcher. A host named stoprod-db01 will be split up to the tag-groups sto, prod, stoprod and db.

We then create a group var under the group company called graphite_hostname and assign it a value. Two files, db.yml and www.yml are included under the group company.

inventory/db.yml

---
galera:
  stage:
    vars:
      galera_cluster_name: "galera-stage"
    hosts:
      - stostage-db01
      - stostage-db02
      - lonstage-db01
  prod:
    vars:
      galera_cluster_name: "galera-prod"
    hosts:
      - stoprod-db01
      - stoprod-db02
      - stoprod-db03
      - lonprod-db01
      - lonprod-db02

In theory I could define the variables under group_vars/stage.yml (and prod) and get rid of both vars and the redundant groups stage and prod and just turn everything to a simple list. We choose to keep it like this to make it more clear.

group_vars/db.yml

---

innodb_buffer_pool_size: 4096M
query_cache_size: 32M

These variables will be assigned to all hosts tagged with db.

site.yml


- hosts: db
  roles:
    - galera

- hosts: stage:&db
  tasks:
    - debug: msg="Stage stuff"

I like to use unions like stage:&db. An alternative way to refer to the stage galera machines would be with the group company-galera-stage, or stage:&company-galera.