# Help on Conditionals - When

**URL:** <https://forum.ansible.com/t/help-on-conditionals-when/35212>\
**Category:** Ansible Project\
**Created:** [May 23, 2021, 7:55am UTC](https://forum.ansible.com/t/help-on-conditionals-when/35212 "2021-05-23T07:55:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanoop\_Karat](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sanoop\_Karat](https://forum.ansible.com/u/Sanoop_Karat)\
**Post date:** [May 23, 2021, 7:55am UTC](https://forum.ansible.com/t/help-on-conditionals-when/35212/1 "2021-05-23T07:55:28Z")

</div>

Dear Team,

I have been trying to add conditionals for my tasks and roles.  
But, the one I entered for role is getting evaluated to true always and the one on the tasks are evaluated to false.

Can you help on what needs to be changed.

Below is my playbook

- hosts: sec  
roles:

- role: service-operations  
when: (bName == ‘security’) or (bName == ‘all’)

- hosts: core  
roles:

- role: service-operations  
when: (bName == ‘core’) or (bName == ‘all’)

- hosts: composition  
roles:

- role: service-operations  
when: (bName == ‘composition’) or (bName == ‘all’)

The above playbook is getting executed for all hosts, even though the bName ‘security’.

Below are the tasks

- name: Adding the User account to Log on as a service  
win\_user\_right:  
name: SeServiceLogonRight  
users:

- ‘{{ srvLogOnUsr }}’  
action: add  
when: uselogonUser

- name: Start the service with logon  
win\_service:  
name: “{{ service }}”  
start\_mode: auto  
state: started  
username: “{{ srvLogOnUsr }}”  
password: “{{ srvLogOnPassword }}”  
when:

- opr == “Start”

- uselogonUser == “true”

Both these tasks are gettign skipped even though uselogonUser = true & opr = Start

Can anyone guide me on what is going wrong.

Thanks  
Sanoop

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 23, 2021, 8:34am UTC](https://forum.ansible.com/t/help-on-conditionals-when/35212/2 "2021-05-23T08:34:56Z")

</div>

> Dear Team,
> 
> I have been trying to add conditionals for my tasks and roles.  
> But, the one I entered for role is getting evaluated to true always and

the one on the tasks are evaluated to false.

> Can you help on what needs to be changed.
> 
> Below is my playbook
> 
> - hosts: sec  
> roles:  
> - role: service-operations  
> when: (bName == 'security') or (bName == 'all')
> 
> - hosts: core  
> roles:  
> - role: service-operations  
> when: (bName == 'core') or (bName == 'all')
> 
> - hosts: composition  
> roles:  
> - role: service-operations  
> when: (bName == 'composition') or (bName == 'all')
> 
> The above playbook is getting executed for all hosts, even though the bName 'security'.

Where is bName defined?

> Below are the tasks
> 
> - name: Adding the User account to Log on as a service  
> win\_user\_right:  
> name: SeServiceLogonRight  
> users:  
> - '{{ srvLogOnUsr }}'  
> action: add  
> when: uselogonUser
> 
> - name: Start the service with logon  
> win\_service:  
> name: "{{ service }}"  
> start\_mode: auto  
> state: started  
> username: "{{ srvLogOnUsr }}"  
> password: "{{ srvLogOnPassword }}"  
> when:  
> - opr == "Start"  
> - uselogonUser == "true"
> 
> Both these tasks are gettign skipped even though uselogonUser = true & opr = Start

true (boolean value) is different from "true" (string).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sanoop\_Karat](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sanoop\_Karat](https://forum.ansible.com/u/Sanoop_Karat)\
**Post date:** [May 24, 2021, 3:41am UTC](https://forum.ansible.com/t/help-on-conditionals-when/35212/3 "2021-05-24T03:41:12Z")

</div>

Where is bName defined? - It is defined in the groupvariables vars.yml file

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 24, 2021, 7:07am UTC](https://forum.ansible.com/t/help-on-conditionals-when/35212/4 "2021-05-24T07:07:12Z")

</div>

> Dear Team,
> 
> I have been trying to add conditionals for my tasks and roles.  
> But, the one I entered for role is getting evaluated to true always and the one on the tasks are evaluated to false.
> 
> Can you help on what needs to be changed.
> 
> Below is my playbook
> 
> - hosts: sec  
> roles:
> 
> - role: service-operations  
> when: (bName == ‘security’) or (bName == ‘all’)
> 
> - hosts: core  
> roles:
> 
> - role: service-operations  
> when: (bName == ‘core’) or (bName == ‘all’)
> 
> - hosts: composition  
> roles:
> 
> - role: service-operations  
> when: (bName == ‘composition’) or (bName == ‘all’)
> 
> The above playbook is getting executed for all hosts, even though the bName ‘security’.

But the conditional has an OR in it, so the entire expression will be influenced by two sub expressions.  
You can add debug tasks for both variables to each play section. This will reveal what values are being used, giving a hint as where to look why these variables get overridden.
