การแปลงสตริงเป็นจำนวนเต็มใน Ansible Playbook

Aug 17 2020

ฉันได้รับการนับจากคำสั่ง powershell และลงทะเบียนกับตัวแปร ฉันต้องใช้การนับนั้นในเมื่อเงื่อนไข ฉันได้เปลี่ยนเป็น 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: <[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

คำตอบ

6 RomanSpiak Aug 17 2020 at 05:53

นี่คือสิ่งที่ฉันทำเพื่อให้มันใช้งานได้:

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

และนี่คือผลการรัน 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   

เพื่อสรุป:

  • คุณควรตรวจสอบว่าตัวแปร register ไม่ใช่โครงสร้างที่ซับซ้อนมากขึ้นหรือไม่โดยปกติจะเป็น
  • คุณไม่ต้องการข้อเท็จจริงที่กำหนดเองอีก
  • คุณต้องแปลงตัวแปรของคุณโดยไม่ต้องใช้{{ }}ในwhenเงื่อนไข