I have a setup where I have multiple inventory files in a directory and my ansible.cfg points to that inventory directory rather than a file. Also using ec2.py for aws dynamic inventory which is inside the inventory directory also. All hosts files, except for one are in a sub directory.
All of the host files have [all:vars] defined, pretty much all the variables are duplicated across these different hosts files, but some have different values based on the type of environment. I’m attempting to use Dynamic inventory and to accommodate this I have a ‘hosts’ file named as such, in the root of the inventory directory. This contains all my tagging for AWS to allow me to use short names which relate to an AWS tag + the same [all:vars] which I want to be read when I’m using dynamic inventory, because I don’t want ansible reading other [all:vars] from other inventory files.
Along with the hosts files, I have a group_vars dir, which contains an all.yml that also duplicates some of the variables in the different hosts files(I know, I know). I eventually want to deprecate the [all:vars] contained in the hosts files in favour of just using group_vars/{all,env1,env2, etc}, but at the moment I can’t.
My question is, when I run ansible, in what order will it obtain variables when multiple host files exist, i.e. using dynamic inventory option I don’t specify a hosts file or ec2.py, just allow ansible.cfg to read everything in the inventory dir:
ansible-playbook someplaybook.yml -e ‘somevar=something anothervar=anotherthing’ --limit devenv
Ideally I want ansible to use just read ec2.py and ‘hosts’ file, or at least read the ‘hosts’ and pickup the [all:vars] from there. I don’t want it reading all the other host files that exist in the directory or at least I want to know what order it will read them. What is the precedence?
Does having hosts files in sub-directories help in the precedence of reading the hostfiles?
Thanks.