How to add multiple inventory files in command line while executing a playbook

prasanna kumar picture prasanna kumar · Feb 27, 2019 · Viewed 7.5k times · Source

I am having a playbook with two different plays

Sample.yml
    - name : Play1
      hosts: Host1
      tasks:
       ...
    - name: Play2
      hosts: Host2
      tasks:
       ...

I need to run this playbook with two different hosts(Host1 and Host2) and these two different hosts are present in two separate files(Hostfile1 and Hostfile2) under inventory/ directory.

inventory/
   Hostfile1
   Hostfile2
   .
   .
   HostfileN

I want to know how to include two different hosts file while running the playbook. I know by including the entire folder (inventory/) in command line we can achieve this but I have lot of hosts files inside inventory/ folder so this option will load unused hosts file.

I tried to run like below

ansible-playbook -i inventory/Hostfile1,Hostfile2 sample.yml

But this didn't work. So, do anyone know how to run the playbook by providing multiple hosts file in command line?

Answer

Szczad picture Szczad · Feb 27, 2019

Just simply provide -i multiple times

ansible-playbook -i inventory/Hostfile1 -i inventory/Hostfile2 sample.yml