Chris
Chris

Reputation: 1019

task is skipped even when condition is met

There are following three consecutive tasks in a playbook:

  - debug:
     msg: "{{ cert_result.stdout }}"
    tags:
     - debug

  - name: Certdir rehash
    shell: "cacertdir_rehash /etc/openldap/cacerts/"
    when: cert_result|int == 1
    tags:
     - carehash

  - debug:
     msg: "{{ cert_result.stdout }}"
    tags:
     - debug

I can't explain why the "Certdir rehash" is skipped even if cert_result variable is set to 1

Here output from those three tasks during run:

TASK [debug] ***************************************************************************************************************************************************************
Monday 24 May 2021  23:11:01 +0200 (0:00:02.514)       0:00:08.004 ************
ok: [192.168.1.110] => {
    "msg": "1"
}

TASK [Certdir rehash] ******************************************************************************************************************************************************
Monday 24 May 2021  23:11:01 +0200 (0:00:00.156)       0:00:08.160 ************
skipping: [192.168.1.110]

TASK [debug] ***************************************************************************************************************************************************************
Monday 24 May 2021  23:11:01 +0200 (0:00:00.156)       0:00:08.317 ************
ok: [192.168.1.110] => {
    "msg": "1"
}

Why ansible is skipping this task with when: cert_result|int == 1?

Upvotes: 0

Views: 1325

Answers (2)

P....
P....

Reputation: 18351

you are using debug with cert_result.stdout but for when you are only using cert_result. I think you should use the same as below.

 - name: Certdir rehash
    shell: "cacertdir_rehash /etc/openldap/cacerts/"
    when: cert_result.stdout|int == 1
    tags:
     - carehash

Notice the difference when you used cert_result.stdout vs cert_result, only the former returned "1".

Upvotes: 2

user3781737
user3781737

Reputation: 1012

cert_result is not an integer, it’s a dictionary containing the property stdout which is what you want to typecast. Notice how you’re calling cert_result.stdout in both debug tasks, but not in the one conditional task being skipped.

Change your when into cert_result.stdout | int == 1.

Upvotes: 1

Related Questions