11

I am getting a count from powershell command and registering it on variable. I have to use that count in when condition. I have changed it to int before using it in when condition too. Still that task ( mail notification) is getting skipped, eventhough the count is 0 here. Can someone tell me what I am doing wrong here. Below is the code I am executing

      - name: Get message_count
        shell:  echo "{{ (output.stdout | from_json).MessageCount  }}"
        register: message_count   #message_count is Zero here
        delegate_to: localhost
     
      - set_fact:
          countt: "{{ message_count | int}}"    

#tried converting to integer before passing to condition using set_fact

      - debug: var=countt
      - name: send mail notification
        mail:
           host: abc.zzzz.net
           port: 25
           from: <[email protected]>
           to:
           - [email protected]        

           subject: Test mail sent from core server 
           body: Test mail sent from core server        
        delegate_to: localhost
        when: countt==0
1
  • 1) Display message_count. For example - debug: var=message_count. You'll see that the output of the shell is stored in message_count.stdout (and in message_count.stdout_lines). 2) When you set the countt properly use it in the condition when: countt|int == 0. Aug 16, 2020 at 21:15

1 Answer 1

12

here's what I did to make it work:

---
- name: answer serverfault
  hosts: all
  become: yes

  tasks:
    - name: Get message_count
      shell: ls /tmp/empty | wc -l
      register: tmp_count
      delegate_to: localhost
    - debug: var=tmp_count.stdout
    - name: do something else when tmp_count.stdout == 0
      shell: echo "I am doing it"
      delegate_to: localhost
      when: tmp_count.stdout | int == 0

and here's the playbook run result:

ripper@mini-ripper:~/Devel/ansible$ ansip ./test_playbook.yml  -i localhost,

PLAY [answer serverfault] **************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]

TASK [Get message_count] ******************************************************************************************************************************************************************************************
changed: [localhost -> localhost]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "tmp_count.stdout": "0"
}

TASK [do something else when tmp_count.stdout == 0] ***************************************************************************************************************************************************************
changed: [localhost -> localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

so to recap:

  • you should check if the register variable isn't more complex structure - it usually is
  • you don't need another custom fact
  • you need to convert your variable without using {{ }} in when condition
4
  • This seems to just convert a string var into int just before comparison, but how can we get an int variable in the first place, so that we do not have to convert in situ each time we use it? Mar 9, 2023 at 3:48
  • You can introduce a custom fact that is populated by the converted int value and then just use that if needed. - name: Setting fact ansible.builtin.set_fact: one_fact: tmp_count.stdout | int Mar 16, 2023 at 7:08
  • Have you tried it? It does not work Mar 16, 2023 at 18:46
  • true... I've just tried it. Referencing another SF question here: stackoverflow.com/questions/70259063/… . The output of Jinja is always string. It's a template designer. - author @vladimir botka. Mar 19, 2023 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .