# How can load an inventory file via a task within a playbook

**URL:** <https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772>\
**Category:** Ansible Project\
**Created:** [June 20, 2017, 2:00pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772 "2017-06-20T14:00:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [June 20, 2017, 2:00pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772/1 "2017-06-20T14:00:36Z")

</div>

I keep my inventory files for each environment in GIT repositories. I am working on some sort of a helper service which i am writing in Ansible and this should perform some (housekeeping) tasks on the specified servers, which i wish to specify using existing inventory files. I want to make the GIT repo as a parameter for this script and then the script will have its target inventory file. I am not sure how to proceed from here.

How can i load this inventory file within a task ? After loading, i can either invoke next play or refresh the inventory with meta. Using add\_host doesn’t seem to be an option as there are elaborate inventory configurations which i want to reuse as it is rather than writing more logic to load it.I don’t see any lookup/parser either. The only option remains for me is to keep the second phase logic in different playbook and after fetching the inventory just shoot a shell command executing this playbook with the inventory, but this method won’t give me the logs like a regular playbook execution.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 20, 2017, 3:22pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772/2 "2017-06-20T15:22:22Z")

</div>

Can't you just pull the git repo before you run the inventory?

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [June 20, 2017, 3:37pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772/3 "2017-06-20T15:37:32Z")

</div>

I want to make it useful for others, thus trying to cut on an additional GIT clone step. I will create a Jenkins job for this which would then just need a parameter (with which i can figure out GIT repo).

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 21, 2017, 3:52pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772/4 "2017-06-21T15:52:50Z")

</div>

Why wouldn't you have a single repo in that case?  
You can have multiple inventory folders under the playbooks e.g.

.├── roles \< - common roles  
│ ├── bar  
│ └── foo  
├── site.yml \<- common playbooks  
├── dev \<- dev inventory  
│ ├── group\_vars  
│ │ └── all  
│ └── hosts  
├── prod \<- production inventory  
│ ├── group\_vars  
│ │ └── all  
│ └── hosts

└── staging \<- staging inventory  
&nbsp;&nbsp;&nbsp;&nbsp;├── group\_vars  
&nbsp;&nbsp;&nbsp;&nbsp;│ └── all  
&nbsp;&nbsp;&nbsp;&nbsp;└── hosts

Then your jenkins play just checks out the repo and runs

ansible-playbook -i dev/ site.yml

(i.e. just switch the inventory to the right one).

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [June 21, 2017, 4:00pm UTC](https://forum.ansible.com/t/how-can-load-an-inventory-file-via-a-task-within-a-playbook/24772/5 "2017-06-21T16:00:40Z")

</div>

Hi,

This has to do how we are managing inventory as per project requirements. We have some quite elaborate structure and it is now tried and tested. What i am doing now is automating routines stuff with Ansible and this cannot result in changing the existing structure. I gotta find ways with the current setup fixed.

And anyways, i would like to get a general answer if that would be possible, not just to solve my current problem but also to know that we have this ‘possibility’ in Ansible so that i can go on think of more automation ideas.
