# ansible --list-hosts behaviour

**URL:** <https://forum.ansible.com/t/ansible-list-hosts-behaviour/26019>\
**Category:** Ansible Project\
**Created:** [November 14, 2017, 2:27pm UTC](https://forum.ansible.com/t/ansible-list-hosts-behaviour/26019 "2017-11-14T14:27:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aivars\_Sterns](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Aivars\_Sterns](https://forum.ansible.com/u/Aivars_Sterns)\
**Post date:** [November 14, 2017, 2:27pm UTC](https://forum.ansible.com/t/ansible-list-hosts-behaviour/26019/1 "2017-11-14T14:27:18Z")

</div>

ansible --list-hosts behaviour is something strange, at least it works as i don’t expect, it differs from ansible 2.3 to ansible 2.4

test case:  
created inventory  
`  
[group]  
host1-c1  
host2

[group1-c1]  
host3  
host4

[group2-c1]  
host5  
host6  
host11-c1

`

2.3 ansible behaviour is to return all hosts and groups

`  
ansible–list-hosts “\*c1”  
hosts (6):  
host11-c1  
host1-c1  
host5  
host6  
host3  
host4

`

2.4 ansible behaviour is to return hosts in groups if some group matches \*c1 and if no match to group then return hosts

`  
hosts (5):  
host5  
host6  
host11-c1  
host3  
host4

`

and if i add “\*1-c1” to parameter:  
2.3 ansible:

`  
ansible -i i --list-hosts “\*1-c1”  
hosts (4):  
host11-c1  
host1-c1  
host3  
host4

`

2.4 ansible:

`ansible -i i --list-hosts "*1-c1" `hosts (2):  
host3  
host4

There is no consistency from version to version, and now i’m confused how it should be correctly. My version is that it should only return hosts in groups that match that expression, but what if i add one more symbol to expression “11-c1” both return just host, even there are no group which ends with 11-c1.

when i add such expression i should not be aware of matching hosts/groups if it returns hosts from groups, then this is fail, if it’s return all matches in groups and hosts then it’s fail again. When i add expression i cannot guarantee that someone else is not added some host with that match, or there is no more such group and i execute on some host which matches that expression not group.

so can someone explain the behaviour i observer and provide correct answer how it should work and how we can rely on this.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2017, 8:29pm UTC](https://forum.ansible.com/t/ansible-list-hosts-behaviour/26019/2 "2017-11-16T20:29:11Z")

</div>

Don't take this as cannon, I would like to raise this in core team  
meeting, but until then here are my thoughts:

This is a bit complicated, in general a 'host pattern' should return a  
group if it matches, if not it should try to match hosts.  
That said, that is meant for 'string' patterns not '\*glob' patterns,  
which should match 'anything that matches', rendering the 2.3  
behaviour as 'correct' in my interpretation.

Just to make it clear, for the first case 'string' it should not match  
the host 'c1', only the group:

[c1]  
hosta  
hostb

[dev]  
c1

should return:  
hosta  
hostb

but for the 2nd case '\*1' should match both host and group:

hosta  
hostb  
c1

---

<div class="post-metadata">

**Author:** ![Aivars\_Sterns](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Aivars\_Sterns](https://forum.ansible.com/u/Aivars_Sterns)\
**Post date:** [November 17, 2017, 9:23am UTC](https://forum.ansible.com/t/ansible-list-hosts-behaviour/26019/3 "2017-11-17T09:23:29Z")

</div>

> So this is bug then do i need to report that in ansible 2.4 it does not work as expected ?
