Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible multiple inventory file

Tags:

ansible

I am trying to use multiple inventory file and dynamic inventory with Ansible 1.4 and dev. Ansible returns No hosts matched.

I have a simulated scenario with two hosts file in a directory test the content of the directory is listed.

hosts1.ini

[group1]
test1    ansible_ssh_host=127.0.0.1
test2    ansible_ssh_host=127.0.0.2
[group2]
test3    ansible_ssh_host=127.0.0.3

hosts2.ini

[group3]
test4     ansible_ssh_host=127.0.0.4
[group4]
test5    ansible_ssh_host=127.0.0.4
test6    ansible_ssh_host=127.0.0.5

if I run ansible -i test --list-hosts all it returns No hosts matched.

I digged into the code and found dir.py with a small amended i got it too work. But I think i must have done something wrong and the hack is not required. Any ideas on how to solve it ?

like image 482
DomaNitro Avatar asked Feb 07 '14 22:02

DomaNitro


People also ask

Can Ansible have multiple inventory files?

Using inventory directories and multiple inventory sourcesIf the location given to -i in Ansible is a directory (or as so configured in ansible. cfg ), Ansible can use multiple inventory sources at the same time.

Is it possible to specify multiple inventory files at once?

You can also use multiple inventory files at the same time as described in Using multiple inventory sources, and/or pull inventory from dynamic or cloud sources or different formats (YAML, ini, and so on), as described in Working with dynamic inventory.

How do I use different inventory files in Ansible?

Ansible works against multiple systems in your infrastructure at the same time. It does this by selecting portions of systems listed in Ansible's inventory file, which defaults to being saved in the location /etc/ansible/hosts . You can specify a different inventory file using the -i <path> option on the command line.


2 Answers

Remove the .ini from your file names:

$ ls test/
hosts1  hosts2

$ ansible -i test --list-hosts all 
    test1
    test2
    test3
    test5
    test6
    test4
like image 198
klenwell Avatar answered Oct 19 '22 19:10

klenwell


If you come to this question and want to use multiple inventories from different locations, just specify the -i parameter multiple times:

ansible -i test -i another/path/to/inventory --list-hosts all

Be aware, that the group vars of the last specified inventory will be used for all inventories.

like image 1
stackprotector Avatar answered Oct 19 '22 20:10

stackprotector