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

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.

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

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

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

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.