Search code examples
ansibleansible-inventory

Ansible | list hostgroups which has shared hosts


We have use case to deploy applications using Ansible, ansible inventory has multiple hostgroups and in some cases same hosts are shared across multiple hostgroups

inventoryfile.ini as below :

[prod-apphostgroup1]
host01.local given_name=host01
host02.local given_name=host02

[prod-apphostgroup2]
host01.local given_name=host01
host02.local given_name=host02

Deployment is happening on Ubuntu machines. In any case if we are deploying to these hostgroups we cannot run in parallel as the apt lock issue happens which in turn marks deployment as failed.

I am trying to do the following :

  1. Setup up deploy order for list for list of applications, this list is static

    Level1

    • application 1 (uses hostgroup1)
    • application 2 (uses hostgroup1) Level2
    • application 3 (uses hostgroup2)
    • application 4 (uses hostgroup4)
  2. While parsing the list if any application uses same hostgroups then it should change the Level. In my case hostgroup1 used in application1 and application2 at Level1. So application2 deployment has to move to Level2

In order to update the deploy order lookup i need to find if any of the hostgroup is shared in the inventory. I am doing deployment to multiple sites, in some sites each application is associated with unique hostgroup and in some sites its shared.

Is there any way in ansible to find list of hostgroups that has hosts shared with other hostgroups in one particular site (inventory file)

Any reference to similar kind of problem or documentation is much appreciated.

Thanks in advance !


Solution

  • To answer your question

    Is there any way in ansible to find list of hostgroups that has hosts shared with other hostgroups in one particular site (inventory file)?

    No. There isn't, you may parse the inventory file on your own. All groups and hosts in Ansible are available in the variable groups. See

    - debug: var=groups
    

    The are Filters available to find intersect and difference of 2 groups.