Ansible Playbook で文字列を整数に変換する

Ansible Playbook で文字列を整数に変換する

PowerShell コマンドからカウントを取得し、変数に登録しています。そのカウントを when 条件で使用する必要があります。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: <[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

答え1

これを機能させるために私が行ったことは次のとおりです:

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

関連情報