Chuyển đổi chuỗi thành số nguyên trong Ansible Playbook

Aug 17 2020

Tôi nhận được số lượng từ lệnh powershell và đăng ký nó trên biến. Tôi phải sử dụng số đếm đó trong điều kiện khi nào. Tôi đã thay đổi nó thành int trước khi sử dụng nó trong điều kiện khi quá. Vẫn còn tác vụ đó (thông báo qua thư) đang bị bỏ qua, mặc dù số lượng ở đây là 0. Ai đó có thể cho tôi biết những gì tôi đang làm sai ở đây. Dưới đây là đoạn mã tôi đang thực thi

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

# đã thử chuyển đổi thành số nguyên trước khi chuyển sang điều kiện bằng 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

Trả lời

6 RomanSpiak Aug 17 2020 at 05:53

đây là những gì tôi đã làm để làm cho nó hoạt động:

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

và đây là kết quả chạy 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   

để tóm tắt lại:

  • bạn nên kiểm tra xem biến thanh ghi có cấu trúc phức tạp hơn không - nó thường là
  • bạn không cần một thực tế tùy chỉnh khác
  • bạn cần chuyển đổi biến của mình mà không cần sử dụng {{ }}trong whenđiều kiện