Ansible

在 Ansible Playbook 中將字元串轉換為整數

  • August 16, 2020

我從 powershell 命令中獲取計數並將其註冊到變數上。我必須在條件下使用該計數。在 when 條件下使用它之前,我已將其更改為 int 。儘管此處的計數為 0,但仍會跳過該任務(郵件通知)。有人可以告訴我我在這裡做錯了什麼。下面是我正在執行的程式碼

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

#在使用 set_fact 傳遞給條件之前嘗試轉換為整數

     - debug: var=countt
     - name: send mail notification
       mail:
          host: abc.zzzz.net
          port: 25
          from: <noreply@controlnode.com>
          to:
          - abc@xyz.com        

          subject: Test mail sent from core server 
          body: Test mail sent from core server        
       delegate_to: localhost
       when: countt==0

這是我為使其工作所做的工作:

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

這是劇本的執行結果:

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   

回顧一下:

  • 您應該檢查寄存器變數是否不是更複雜的結構-通常是
  • 你不需要另一個自定義事實
  • 您需要在不使用{{ }}條件的when情況下轉換變數

引用自:https://serverfault.com/questions/1030350