Search code examples
ansibleansible-inventory

Configure servers pairwise from two inventory groups in Ansible


Eventual system setup

System setup as in pic above.

Inventory File

[group1]
host1
host2
host3

[group2]
host4
host5
host6

What I want to do is, when I configure hosts from group1 with some roleA, I want to trigger roleB for group2 hosts pairwise, such that-

  • host4 is configured with host1.
  • host5 is configured with host2 etc.

Here, host1 will also be used as a variable in host4 config file

basically, pairwise configurations (host1, host4), (host2, host5) etc. Is this possible in Ansible? Please give examples, I have been stuck on this for quite a bit now


Solution

  • Basically you can define your inventory with host_vars as below::

      [server_set1]
      test1.labserver.com  server_pair=test4.labserver.com
      test2.labserver.com  server_pair=test5.labserver.com
      test3.labserver.com  server_pair=test6.labserver.com
    
      [server_set2]
      test4.labserver.com 
      test5.labserver.com  
      test6.labserver.com
    

    Then anytime you run your playbook, You have ability to fetch the variable "server_pair" and run your role B against its pairing server.

    ansible -m debug -a "msg={{ server_pair }}" test3.labserver.com -i inventory

    test3.labserver.com | SUCCESS => { "changed": false, "msg": "test6.labserver.com" }

    Ansible doesn't provide any mechanism to achieve this in a automated way, In case if you are looking for such type of solution. You have to get the pair
    in a programmatic way.