Conversione di stringhe in numeri interi in Ansible Playbook

Aug 17 2020

Ricevo un conteggio dal comando PowerShell e lo registro su variabile. Devo usare quel conteggio in quando condizione. L'ho cambiato in int prima di usarlo anche nella condizione when. Tuttavia quell'attività (notifica e-mail) viene saltata, anche se qui il conteggio è 0. Qualcuno può dirmi cosa sto facendo male qui. Di seguito è riportato il codice che sto eseguendo

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

#ha provato a convertire in numero intero prima di passare a condizione 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

Risposte

6 RomanSpiak Aug 17 2020 at 05:53

ecco cosa ho fatto per farlo funzionare:

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

ed ecco il risultato dell'esecuzione del playbook:

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   

quindi ricapitolando:

  • dovresti controllare se la variabile register non è una struttura più complessa - di solito lo è
  • non hai bisogno di un altro fatto personalizzato
  • devi convertire la tua variabile senza usare {{ }}in whencondition