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

43 views
Skip to first unread message

ishan jain

unread,
Jun 20, 2017, 10:00:36 AM6/20/17
to Ansible Project
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.

Dick Davies

unread,
Jun 20, 2017, 11:24:37 AM6/20/17
to ansible list
Can't you just pull the git repo before you run the inventory?
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4e2c79d8-7190-4050-9977-ad994032794f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

ishan jain

unread,
Jun 20, 2017, 11:37:33 AM6/20/17
to Ansible Project, di...@hellooperator.net
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).

Dick Davies

unread,
Jun 21, 2017, 11:52:58 AM6/21/17
to ishan jain, Ansible Project
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
├── group_vars
│ └── all
└── 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).

ishan jain

unread,
Jun 21, 2017, 12:00:40 PM6/21/17
to Ansible Project, ishan...@gmail.com, di...@hellooperator.net
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.
Reply all
Reply to author
Forward
0 new messages