ansible --list-hosts behaviour

638 views
Skip to first unread message

Aivars Šterns

unread,
Nov 15, 2017, 7:29:25 PM11/15/17
to Ansible Project
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. 


Brian Coca

unread,
Nov 16, 2017, 3:29:32 PM11/16/17
to Ansible Project
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

----------
Brian Coca

Aivars Šterns

unread,
Nov 17, 2017, 4:23:29 AM11/17/17
to Ansible Project
So this is bug then do i need to report that in ansible 2.4 it does not work as expected ?
Reply all
Reply to author
Forward
0 new messages