# Role played on all hosts, condition  "when: 'group' in group\_names" not correctly evaluated

**URL:** <https://forum.ansible.com/t/role-played-on-all-hosts-condition-when-group-in-group-names-not-correctly-evaluated/22616>\
**Category:** Ansible Project\
**Created:** [September 22, 2016, 2:35pm UTC](https://forum.ansible.com/t/role-played-on-all-hosts-condition-when-group-in-group-names-not-correctly-evaluated/22616 "2016-09-22T14:35:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerie\_P](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@Valerie\_P](https://forum.ansible.com/u/Valerie_P)\
**Post date:** [September 22, 2016, 2:35pm UTC](https://forum.ansible.com/t/role-played-on-all-hosts-condition-when-group-in-group-names-not-correctly-evaluated/22616/1 "2016-09-22T14:35:20Z")

</div>

Hi,

I am using Ansible 2.2.0 to configure network devices. I have a problem with the evaluation of a condition using "when: ‘group’ in group\_names’.

The playbook is :

`

- name: Configure topologies  
hosts: “{{ TOPO }}”  
strategy: debug  
vars\_files:
- “vars\_file/{{TOPO}}.yml”  
roles:
- set\_ce\_bgp\_params
- set\_pe\_bgp\_params
- set\_sw\_vlan
- set\_banner  
tags:
- config

`

Where TOPO is an extra vars I provide when I launch the play. Here TOPO is topo14. The host list contains the following items :

`

[ce]  
rtr-site1  
rtr-site2-1  
rtr-site2-2  
rtr-site2-tagin-1  
rtr-site2-tagin-2  
rtr-site3-1  
rtr-site3-2  
rtr-site4-1  
rtr-site4-2  
rtr-site5  
rtr-site6

[pe]  
rtr-ipsn  
rtr-pe1  
rtr-pe2

[topo14]  
rtr-site6  
rtr-site2-1  
rtr-site2-2  
rtr-pe1  
rtr-pe2  
sw-site1  
sw-site3  
ch-site2-qa-1  
ch-site6-qa

`

I have a problem with this role :

`

- name: Update neighbor parameters  
connection: local  
ios\_config:  
host: “{{ inventory\_hostname }}”  
parents:
- “router bgp {{ bgp\_as }}”
- “address-family ipv4 vrf {{ vrf }}”  
commands:
- “neighbor {{ item.value }} {{topo[rtr\_site][rtr\_index][‘bgp\_actions’][item.key]}}”  
with\_dict: bgp\_neighbors  
when:
- “‘ce’ in group\_names”
- not (  
(topo[rtr\_site][rtr\_index][‘bgp\_actions’][item.key] is undefined) or  
(topo[rtr\_site][rtr\_index][‘bgp\_actions’][item.key] | trim == “”)  
)  
notify: clear ip bgp  
`

The role is supposed to be used only on hosts belonging to the [ce] group, but when I launch the play here is the output :

`  
PLAY [Configure topologies] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [set\_ce\_bgp\_params : Update neighbor parameters] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

fatal: [sw-site3]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

fatal: [sw-site1]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

fatal: [ch-site2-qa-1]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

fatal: [ch-site6-qa]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

fatal: [rtr-pe2]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

fatal: [rtr-pe1]: FAILED! =\> {“failed”: true, “msg”: “with\_dict expects a dict”}

changed: [rtr-site6] =\> (item={‘key’: u’ipsn’, ‘value’: u’57.0.0.9’})

skipping: [rtr-site6] =\> (item={‘key’: u’pe1’, ‘value’: u’10.16.11.2’})

changed: [rtr-site2-1] =\> (item={‘key’: u’ipsn’, ‘value’: u’57.0.0.9’})

changed: [rtr-site2-2] =\> (item={‘key’: u’ipsn’, ‘value’: u’57.0.0.9’})

skipping: [rtr-site2-2] =\> (item={‘key’: u’hsrp’, ‘value’: u’10.12.0.1’})

skipping: [rtr-site2-2] =\> (item={‘key’: u’pe2’, ‘value’: u’10.12.22.2’})

ok: [rtr-site6] =\> (item={‘key’: u’pe2’, ‘value’: u’10.16.12.2’})

ok: [rtr-site2-1] =\> (item={‘key’: u’pe1’, ‘value’: u’10.12.11.2’})

skipping: [rtr-site2-1] =\> (item={‘key’: u’pe2’, ‘value’: u’10.12.12.2’})

skipping: [rtr-site2-1] =\> (item={‘key’: u’hsrp’, ‘value’: u’10.12.0.2’})

`

The role is played even if rtr-pe1/rtr-pe2 and else are not in [ce] group.

What am I doing wrong? Can I do this in an other way?

Kind regards,

Valerie

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 22, 2016, 4:46pm UTC](https://forum.ansible.com/t/role-played-on-all-hosts-condition-when-group-in-group-names-not-correctly-evaluated/22616/2 "2016-09-22T16:46:59Z")

</div>

a) you cannot make a role conditional, any ‘when’ is just applied to the tasks in the role  
b) with\_ runs before when: (so you can make execution conditional by item)  
c) use |default({}) to skip the task and avoid the error

---

<div class="post-metadata">

**Author:** ![Valerie\_P](https://avatars.discourse-cdn.com/v4/letter/v/f475e1/32.png) [@Valerie\_P](https://forum.ansible.com/u/Valerie_P)\
**Post date:** [September 23, 2016, 4:06pm UTC](https://forum.ansible.com/t/role-played-on-all-hosts-condition-when-group-in-group-names-not-correctly-evaluated/22616/3 "2016-09-23T16:06:28Z")

</div>

Thank you very much Brian!
