Ansible task between different group of hosts

Hi guys,

I have this specific scenario where we are trying to do replication between cross sites:

hosts file:
#Site1
[hosts1]
11.22.33.11
11.22.33.12

Site2

[hosts2]

12.22.33.11
12.22.33.12

[hosts_group]
hosts1
hosts2

The role defined needs to be performed between one host of hosts1 group and one host of hosts2 group (11.22.33.11 and 12.22.33.11 ) or ( 11.22.33.12 and 12.22.33.12):

replication.yml file:

  • hosts: hosts1:hosts2
    role: main.yml role

main.yml file:

  • name: Setup replication “{{ansible_play_batch.0}}” host_1 and “{{ansible_play_batch.2}}” host_2
    command: ‘/bin/replication configure --adminUID adminUser --adminPassword password --host1 “{{ansible_play_batch.0}}” --port1 3446 --bindDn1 “{{user_dn}}” --bindPassword1 “{{opendj_root_user_password}}”
    –replicationPort1 “{{replication_port}}” --host2 “{{ansible_play_batch.2}}” --port2 3446 --bindDn2 “{{user_dn}}” --bindPassword2 password --replicationPort2 8888 --baseDN “{{base_dn}}” --trustAll --no-prompt’
    become: yes
    when: ( inventory_hostname == hosts1 hosts1 )
    tags: cross_site_replication
    become_user: “{{test_user}}”
    ignore_errors: yes

Can anyone help with how to ensure the hosts are selected correctly between cross sites (as ansible selects hosts in any order). So that It doesn’t use hosts1[1] as {{ ansible_play_batch.2 }} when {ansible_play_batch.0}==hosts1[0] just because of the order. ?

Thanks,
Arka