# Ansible inventory duplicate hosts in different groups - Ansible picks the last one from the duplicates

**URL:** <https://forum.ansible.com/t/ansible-inventory-duplicate-hosts-in-different-groups-ansible-picks-the-last-one-from-the-duplicates/33629>\
**Category:** Ansible Project\
**Created:** [August 17, 2020, 2:16pm UTC](https://forum.ansible.com/t/ansible-inventory-duplicate-hosts-in-different-groups-ansible-picks-the-last-one-from-the-duplicates/33629 "2020-08-17T14:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@Abhishek\_Sharma](https://forum.ansible.com/u/Abhishek_Sharma)\
**Post date:** [August 17, 2020, 2:16pm UTC](https://forum.ansible.com/t/ansible-inventory-duplicate-hosts-in-different-groups-ansible-picks-the-last-one-from-the-duplicates/33629/1 "2020-08-17T14:16:00Z")

</div>

Hi

I have an inventory defined as below:

[A]  
[host1.example.com](http://host1.example.com) lhost=1010  
[host2.example.com](http://host2.example.com) lhost=1110

[B]  
[host1.example.com](http://host1.example.com) lhost=2010

now when i run below command on group A for [host1.example.com](http://host1.example.com) ansible picks up the host in group B

# ansible -i inventory A -m shell -a “ls -l /tmp/{{ lhost }}”

**output**

ls: no such file or directory /tmp/2010

expected: Ansible should execute on host1 in group A and not group B

Is this behaviour intentional or am i running into some bug or is there something wrong with the command execution.

---

<div class="post-metadata">

**Author:** ![Abhishek\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@Abhishek\_Sharma](https://forum.ansible.com/u/Abhishek_Sharma)\
**Post date:** [August 17, 2020, 2:17pm UTC](https://forum.ansible.com/t/ansible-inventory-duplicate-hosts-in-different-groups-ansible-picks-the-last-one-from-the-duplicates/33629/2 "2020-08-17T14:17:06Z")

</div>

updated command is

# ansible -i inventory A -m shell -a “ls -l /tmp/{{ lhost }}” --limit [host1.example.com](http://host1.example.com)

---

<div class="post-metadata">

**Author:** ![acozine1](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@acozine1](https://forum.ansible.com/u/acozine1)\
**Post date:** [August 17, 2020, 9:46pm UTC](https://forum.ansible.com/t/ansible-inventory-duplicate-hosts-in-different-groups-ansible-picks-the-last-one-from-the-duplicates/33629/3 "2020-08-17T21:46:09Z")

</div>

The host [host1.example.com](http://host1.example.com) is listed in both group A and group B - the only difference is the value of the variable ‘lhost’. Ansible is executing on the correct host, [host1.example.com](http://host1.example.com), but with the variable value from the list ing group B. See [https://docs.ansible.com/ansible/devel/user\_guide/intro\_inventory.html#how-variables-are-merged](https://docs.ansible.com/ansible/devel/user_guide/intro_inventory.html#how-variables-are-merged) for information on how Ansible applies variable values.

If that’s a typo and they are different hosts, just correct the inventory entries and the problem will solve itself. If for some reason you want two entries in your inventory for hte same host, you can still give them different aliases - host1on1010 and host1on22010. Or you can store the variable values in a playbook or vars file and select the correct one at run time.

Hope this helps,  
Alicia
