AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1030350
Accepted
saffron
saffron
Asked: 2020-08-17 11:15:42 +0800 CST2020-08-17 11:15:42 +0800 CST 2020-08-17 11:15:42 +0800 CST

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

  • 772

我从 powershell 命令中获取计数并将其注册到变量上。我必须在条件下使用该计数。在 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
ansible ansible-playbook ansible-tower
  • 1 1 个回答
  • 29964 Views

1 个回答

  • Voted
  1. Best Answer
    Roman Spiak
    2020-08-17T14:53:39+08:002020-08-17T14:53:39+08:00

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

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

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve