Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hosts: [] is matching 'all' hosts #37894

Closed
miramar-labs opened this issue Mar 25, 2018 · 8 comments · Fixed by #37931
Closed

hosts: [] is matching 'all' hosts #37894

miramar-labs opened this issue Mar 25, 2018 · 8 comments · Fixed by #37931
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@miramar-labs
Copy link

miramar-labs commented Mar 25, 2018

ISSUE TYPE
  • Bug Report
COMPONENT NAME

lib/ansible/plugins/filters/mathstuff.py

Set Theory Filters - difference()

ANSIBLE VERSION
ansible 2.4.0.0
  config file = None
  configured module search path = [u'/Users/aaron/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/aaron/ha-dev/venv/ans24-pyvmomi/lib/python2.7/site-packages/ansible
  executable location = ha-dev/venv/ans24-pyvmomi/bin/ansible
  python version = 2.7.14 (default, Feb 26 2018, 20:28:39) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]

CONFIGURATION

N/A

OS / ENVIRONMENT

N/A

SUMMARY

this expression:

{{ groups['masters'] | difference([groups['masters'][0]]) }}

does not produce an empty set if there is only one item in the masters group.
for >1 items in the group, it works as expected.

STEPS TO REPRODUCE
- name: 'test'
  hosts: "{{ groups['masters'] | difference([groups['masters'][0]]) }}"
  gather_facts: no
  tasks:
    - debug: msg="item - {{inventory_hostname}}"

hosts file:
[nodes]
master1.foo.com ansible_host=192.168.0.51 
worker1.foo.com ansible_host=192.168.0.56
[masters]
master1.foo.com ansible_host=192.168.0.51 
[workers]
worker1.foo.com ansible_host=192.168.0.56
[dns]
ns1.foo.com ansible_host=192.168.0.254
[vip]
k8s.foo.com ansible_host=192.168.0.244
[collector]
collector.foo.com ansible_host=192.168.0.50
EXPECTED RESULTS

that the play does not iterate over any hosts, as I think difference() in this case should prodce an empty set.

ACTUAL RESULTS

play iterates over ALL of the hosts:
PLAY RECAP *********************************************************************
k8s.foo.com : ok=1 changed=0 unreachable=0 failed=0
master1.foo.com : ok=1 changed=0 unreachable=0 failed=0
ns1.foo.com : ok=1 changed=0 unreachable=0 failed=0
worker1.foo.com : ok=1 changed=0 unreachable=0 failed=0


@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Mar 25, 2018
@alikins
Copy link
Contributor

alikins commented Mar 26, 2018

pinging @bcoca, @abadger

@alikins alikins removed the needs_triage Needs a first human triage before being processed. label Mar 26, 2018
@bcoca bcoca changed the title Set Theory Filter - difference - bug? hosts: [] is matching 'all' hosts Mar 26, 2018
@bcoca
Copy link
Member

bcoca commented Mar 26, 2018

@miramar-labs the difference filter was not the problem, hosts: [] produces the same issue, its equivalent to hosts: all. The PR above fixes the issue in my testing, please confirm it does the same for you.

@miramar-labs
Copy link
Author

I created a new ans24 venv, replaced manager.py with the raw from above (f0dfbba) and re-ran the test .. same thing...

@miramar-labs
Copy link
Author

hosts file:

[nodes]
master1.foo.com ansible_host=192.168.0.51
worker1.foo.com ansible_host=192.168.0.56

[masters]
master1.foo.com ansible_host=192.168.0.51
#master2.foo.com ansible_host=192.168.0.51

[workers]
worker1.foo.com ansible_host=192.168.0.56

[dns]
ns1.foo.com ansible_host=192.168.0.254

[vip]
k8s.foo.com ansible_host=192.168.0.244

[collector]
collector.foo.com ansible_host=192.168.0.50

@miramar-labs
Copy link
Author

test.yml:

  • name: 'test'
    hosts: "{{ groups['masters'] | difference([groups['masters'][0]]) }}"
    gather_facts: no
    tasks:
    • debug: msg="item - {{inventory_hostname}}"

@miramar-labs
Copy link
Author

result:

PLAY RECAP *********************************************************************
collector.foo.com : ok=1 changed=0 unreachable=0 failed=0
k8s.foo.com : ok=1 changed=0 unreachable=0 failed=0
master1.foo.com : ok=1 changed=0 unreachable=0 failed=0
ns1.foo.com : ok=1 changed=0 unreachable=0 failed=0
worker1.foo.com : ok=1 changed=0 unreachable=0 failed=0

@miramar-labs
Copy link
Author

I may not be testing this correctly ... but i thought that replacing manager.py under site-packages/ansible/inventory should do it ... ?

@bcoca
Copy link
Member

bcoca commented Mar 27, 2018

This is what i get when i run the same with the patch:

bcoca@erebos:~/work/ansible(empty_is_empty)$ ANSIBLE_NOCOWS=1 ansible-playbook play.yml -i ./test.ini
PLAY [test] **********************************************************************************************************************************************************
skipping: no hosts matched

PLAY RECAP ***********************************************************************************************************************************************************

without the patch:

bcoca@erebos:~/work/ansible(devel)$ ANSIBLE_NOCOWS=1 ansible-playbook play.yml -i ./test.ini

PLAY [test] **********************************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************************
ok: [k8s.foo.com] => {
    "msg": []
}
ok: [worker1.foo.com] => {
    "msg": []
}
ok: [master1.foo.com] => {
    "msg": []
}
ok: [collector.foo.com] => {
    "msg": []
}
ok: [ns1.foo.com] => {
    "msg": []
}

TASK [debug] *********************************************************************************************************************************************************
ok: [master1.foo.com] => {
    "msg": "item - master1.foo.com"
}
ok: [k8s.foo.com] => {
    "msg": "item - k8s.foo.com"
}
ok: [worker1.foo.com] => {
    "msg": "item - worker1.foo.com"
}
ok: [collector.foo.com] => {
    "msg": "item - collector.foo.com"
}
ok: [ns1.foo.com] => {
    "msg": "item - ns1.foo.com"
}

PLAY RECAP ***********************************************************************************************************************************************************
collector.foo.com          : ok=2    changed=0    unreachable=0    failed=0
k8s.foo.com                : ok=2    changed=0    unreachable=0    failed=0
master1.foo.com            : ok=2    changed=0    unreachable=0    failed=0
ns1.foo.com                : ok=2    changed=0    unreachable=0    failed=0
worker1.foo.com            : ok=2    changed=0    unreachable=0    failed=0

bcoca added a commit to bcoca/ansible that referenced this issue Apr 10, 2018
bcoca added a commit to bcoca/ansible that referenced this issue Apr 10, 2018
bcoca added a commit to bcoca/ansible that referenced this issue Apr 27, 2018
bcoca added a commit that referenced this issue Apr 30, 2018
ilicmilan pushed a commit to ilicmilan/ansible that referenced this issue Nov 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants