17

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?

prasanna kumar
  • 257
  • 3
  • 4
  • 17

2 Answers2

28

Just simply provide -i multiple times

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

Szczad
  • 787
  • 6
  • 12
  • 3
    So, this is something you MIGHT not want to do. If any of the groupvars are different, it's going to use the last inventory's version for ALL instances – George Griffin Aug 05 '21 at 14:56
5

I wanted to clarify the above answer. The reason the proposal doesn't work is that if ansible sees a ',' in the value of the -i flag, it treats this as an inventory list. Using your example:

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

Ansible will attempt to run the playbook "sample.yml" on the machines "inventory/Hostfile1" and "Hostfile2".

That's why you must specify -i multiple times.

fobriste
  • 51
  • 1
  • 1