nesting of inventory file and include mechanisms

44 views
Skip to first unread message

Narahari 'n' Savitha

unread,
Aug 22, 2017, 10:36:20 PM8/22/17
to Ansible Project
Friends:

This is my first post, so kindly excuse me if anything I post is not a correct understanding.

Thank your for your time and attention to begin with.

Consider this folder and file structure

the_env/
├── ce
│   ├── apa.yml
│   └── cp
│       └── pcb.yml
└── coh.yml

The expected end result  I am looking for is

pcb.yml  includes apa.yml which includes coh.yml


Is this even the right way to think about this ?

or should I write a dynamic inventory script that brings them together ?

-N

Soniya panwar

unread,
Aug 23, 2017, 12:51:22 AM8/23/17
to Ansible Project
yes you can consider this.
but you have to give the full path while including the file otherwise it may give error:

example:
pcb.yml file 

---
- include: /the_env/ce/apa.yml

Narahari 'n' Savitha

unread,
Aug 24, 2017, 8:51:20 AM8/24/17
to Ansible Project
Thank  you Soniya for you time and pointer.

I tried this, but I am not able to make it work for inventory.

playbooks it works but for inventory, I am not sure how to do it

-N

Soniya panwar

unread,
Aug 24, 2017, 9:31:42 AM8/24/17
to Ansible Project
hello,
 it will be easy to understand if you elaborate what exactly error you are getting.


>> playbooks it works but for inventory, I am not sure how to do it

Yes you are right it will work for playbooks but not for inventories.
if you have multiple inventories file then you have to put all your host files within the same inventory folder.



You can use this approach to work with multistage environments by completely separating each operating environment. Instead of maintaining all of your hosts within a single inventory file, an inventory is maintained for each of your individual environments. Separate group_vars directories are also maintained.

The basic directory structure will look something like this:

.
├── ansible.cfg
├── environments/         # Parent directory for our environment-specific directories
│   │
│   ├── dev/              # Contains all files specific to the dev environment
│   │   ├── group_vars/   # dev specific group_vars files
│   │   │   ├── all
│   │   │   ├── db
│   │   │   └── web
│   │   └── hosts         # Contains only the hosts in the dev environment
│   │
│   ├── prod/             # Contains all files specific to the prod environment
│   │   ├── group_vars/   # prod specific group_vars files
│   │   │   ├── all
│   │   │   ├── db
│   │   │   └── web
│   │   └── hosts         # Contains only the hosts in the prod environment
│   │
│   └── stage/            # Contains all files specific to the stage environment
│       ├── group_vars/   # stage specific group_vars files
│       │   ├── all
│       │   ├── db
│       │   └── web
│       └── hosts         # Contains only the hosts in the stage environment
│
├── playbook.yml
│
└── . . .

Narahari 'n' Savitha

unread,
Aug 24, 2017, 1:58:01 PM8/24/17
to Ansible Project
Thank you Soniya for that clarification.
The setup we have is that we have mutliple staging env's.
Between staging environments we have some shared servers.

Example
apache is shared between SE1 (stagin env 1) and SE2

I looked at the structure you have provided and I tried to setup the hosts for that aka inventory.  It works but the result is 
SE1
|
|__hosts (contains apache servers that are common with SE2 plus the tomcat servers that are specific to SE1)

SE2
|
|__hosts(contains apache servers that are common to SE1 plus the tomcat servers that are specific to SE2)

I am trying to avoid duplication.

One option is to use dyanmic inventory approach but I am struggling with writing a python code to do that. 

Soniya panwar

unread,
Aug 24, 2017, 11:31:28 PM8/24/17
to Ansible Project
my understanding of your requirement is still not clear but as far as i understand you are trying to execute playbook for multiple hosts, avoiding duplicacy.
one way to do that is using patterns like below:

The following patterns address one or more groups. Groups separated by a colon indicate an “OR” configuration. This means the host may be in either one group or the other:

webservers
webservers:dbservers

You can also specify the intersection of two groups. This would mean the hosts must be in the group webservers and the host must also be in the group staging:

webservers:&staging
Reply all
Reply to author
Forward
0 new messages