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

Aug 17 2020

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

回答

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

プレイブックの実行結果は次のとおりです。

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