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

孤者浪人 提交于 2019-12-11 00:21:46

问题


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?


回答1:


Just simply provide -i multiple times

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




回答2:


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.



来源:https://stackoverflow.com/questions/54904433/how-to-add-multiple-inventory-files-in-command-line-while-executing-a-playbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!