# Ansible task between different group of hosts

**URL:** <https://forum.ansible.com/t/ansible-task-between-different-group-of-hosts/34166>\
**Category:** Ansible Project\
**Created:** [November 6, 2020, 9:56pm UTC](https://forum.ansible.com/t/ansible-task-between-different-group-of-hosts/34166 "2020-11-06T21:56:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arkaditya\_Verma](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Arkaditya\_Verma](https://forum.ansible.com/u/Arkaditya_Verma)\
**Post date:** [November 6, 2020, 9:56pm UTC](https://forum.ansible.com/t/ansible-task-between-different-group-of-hosts/34166/1 "2020-11-06T21:56:32Z")

</div>

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
