Help on Conditionals - When

19 views
Skip to first unread message

Sanoop Karat

unread,
May 23, 2021, 3:55:28 AM5/23/21
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
May 23, 2021, 4:35:24 AM5/23/21
to ansible...@googlegroups.com
On 5/23/21 9:55 AM, Sanoop Karat wrote:
> 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
Racke

> Can anyone guide me on what is going wrong.
>
>
> Thanks
> Sanoop
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/cb8dd8c5-2307-4738-98f8-77e0d14fcc44n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/cb8dd8c5-2307-4738-98f8-77e0d14fcc44n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Sanoop Karat

unread,
May 23, 2021, 11:41:12 PM5/23/21
to Ansible Project
Where is bName defined? - It is defined in the groupvariables vars.yml file

Dick Visser

unread,
May 24, 2021, 3:07:37 AM5/24/21
to ansible...@googlegroups.com
On Sun, 23 May 2021 at 09:55, Sanoop Karat <sanoops...@gmail.com> wrote:
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.


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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cb8dd8c5-2307-4738-98f8-77e0d14fcc44n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.
Reply all
Reply to author
Forward
0 new messages