在 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情况下转换变量

相关内容