Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 252
prasanna kumar Avatar asked Feb 27 '19 11:02

prasanna kumar


People also ask

Can I have multiple inventory files in Ansible?

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).

How do you define variables in the command line interface while executing playbook?

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).

Which option is used to specify the inventory file in Ansible playbook?

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.

How do I add a new inventory file to my Playbook?

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:

How to use multiple inventory files in Ansible playbooks?

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).

How to merge multiple inventory files into one?

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

How do I specify a different inventory file for each inventory?

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 .


2 Answers

Just simply provide -i multiple times

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

like image 178
Szczad Avatar answered Oct 10 '22 10:10

Szczad


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.

like image 42
fobriste Avatar answered Oct 10 '22 11:10

fobriste