Convertendo string em inteiro no Ansible Playbook

Convertendo string em inteiro no Ansible Playbook

Estou recebendo uma contagem do comando PowerShell e registrando-a na variável. Eu tenho que usar essa contagem quando condição. Eu mudei para int antes de usá-lo na condição when também. Ainda assim, essa tarefa (notificação por email) está sendo ignorada, mesmo que a contagem seja 0 aqui. Alguém pode me dizer o que estou fazendo de errado aqui. Abaixo está o código que estou executando

      - 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}}"    

#tentei converter para número inteiro antes de passar para a condição usando 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

Responder1

aqui está o que fiz para que funcionasse:

---
- 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

e aqui está o resultado da execução do manual:

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   

então, para recapitular:

  • você deve verificar se a variável de registro não é uma estrutura mais complexa - geralmente é
  • você não precisa de outro fato personalizado
  • você precisa converter sua variável sem usar {{ }}na whencondição

informação relacionada