George Shuklin
George Shuklin

Reputation: 7877

How to separate host list and group hiererachy in ansible's inventory?

I have a big project under Ansible. There are many groups in it, most of them for the sake of clarity. For example, there are groups 'dbaccess', 'java', etc, which help to find hosts which need access to database, to have java installed, etc. Some of them nested (f.e. 'collector' group is a child for 'java' group, as all collectors need java).

It all is kept inside inventory using 'group:children' technique.

But when I want to create staging inventory I have a problem: I need not only provide new hosts in inventory, but also recreate whole group hierarchy.

I wonder if there are any way to separate inventory in such a way that group hierarchy would be kept separate from host list?

Or, may be, there is some other way to organize such hierarchy in Ansible?

(example of my real inventory):

[collectors]
(host list)
[publishers-http]
(host list)
[publishers-smtp]
(host list)
[central]
(host list)

[java:children]
collectors
publishers-http
central

[postgre_access:children]
collectors
publishers
central

[mongo_access:children]
collectors
publishers-smtp
central

[redis_access:children]
central
publishers

I want a way to change host lists for staging but keep relations between groups intact.

Upvotes: 1

Views: 1881

Answers (1)

techraf
techraf

Reputation: 68469

Split your inventory into two files -- one named 01_hosts containing:

[collectors]
(host list)
[publishers-http]
(host list)
[publishers-smtp]
(host list)
[central]
(host list)

the other named 02_groups containing:

[java:children]
collectors
publishers-http
central

[postgre_access:children]
collectors
publishers
central

[mongo_access:children]
collectors
publishers-smtp
central

[redis_access:children]
central
publishers

Save them to my_inventory directory, use path to my_inventory as a parameter for the inventory (either in CLI, or in ansible.cfg).

Use symlinks to have a single source for 02_groups.


I suggested 01_ and 02_ prefixes because Ansible reads files from an inventory directory alphabetically and hosts/groups must be defined before groups of groups.

Upvotes: 3

Related Questions