在 Ansible Playbook 中将字符串转换为整数

saf*_*ron 7 ansible ansible-playbook ansible-tower

我从 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}}"    
Run Code Online (Sandbox Code Playgroud)

#在使用 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
Run Code Online (Sandbox Code Playgroud)

小智 7

这是我为使其工作所做的工作:

---
- 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
Run Code Online (Sandbox Code Playgroud)

这是剧本运行结果:

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   
Run Code Online (Sandbox Code Playgroud)

总结一下:

  • 您应该检查寄存器变量是否不是更复杂的结构 - 通常是
  • 你不需要另一个自定义事实
  • 您需要将您的变量转换,而无需使用{{ }}when条件