Conversión de cadena a entero en Ansible Playbook

Aug 17 2020

Obtengo un recuento del comando PowerShell y lo registro en la variable. Tengo que usar esa condición de contar cuando. Lo cambié a int antes de usarlo en la condición when también. Aún así, esa tarea (notificación por correo) se está omitiendo, aunque el recuento es 0 aquí. ¿Puede alguien decirme qué estoy haciendo mal aquí? A continuación se muestra el código que estoy ejecutando

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

#intenté convertir a entero antes de pasar a condición 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

Respuestas

6 RomanSpiak Aug 17 2020 at 05:53

esto es lo que hice para que funcione:

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

y aquí está el resultado de la ejecución del libro de jugadas:

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   

así que para recapitular:

  • debe verificar si la variable de registro no tiene una estructura más compleja; por lo general, lo es
  • no necesitas otro hecho personalizado
  • necesita convertir su variable sin usar {{ }}en whencondición