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

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.


Just simply provide -i multiple times

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