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?
TL;DR: Inventory can be a folder. Create a folder, add as many inventory files inside this folder and instruct Ansible to use this folder as the inventory (with -i folder_name or in your ansible. cfg file). All inventory files inside the folder will be merged into one (including scripts like ec2.py).
You can define variables when you run your playbook by passing variables at the command line using the --extra-vars (or -e ) argument. You can also request user input with a vars_prompt (see Interactive input: prompts).
The default location for inventory is a file called /etc/ansible/hosts . You can specify a different inventory file at the command line using the -i <path> option.
Inside your playbooks folder, create a folder called inventory. Now move your current inventory file (often called hosts) into this folder To tell ansible to use the new inventory, there are 2 options: Create a file ansible.cfg within the same folder as your playbooks and add the lines: Next time you run your playbook, simply add -i inventory:
In this post I will be sharing the way you can use multiple inventory files when running your playbooks and how to organize them. TL;DR: Inventory can be a folder. Create a folder, add as many inventory files inside this folder and instruct Ansible to use this folder as the inventory (with -i folder_name or in your ansible.cfg file).
All inventory files inside the folder will be merged into one (including scripts like ec2.py). Inside your playbooks folder, create a folder called inventory. Now move your current inventory file (often called hosts) into this folder
You can specify a different inventory file at the command line using the -i <path> option. 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 .
Just simply provide -i
multiple times
ansible-playbook -i inventory/Hostfile1 -i inventory/Hostfile2 sample.yml
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With