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 / 问题 / 1147844
Accepted
EricHymowitz
EricHymowitz
Asked: 2023-11-14 22:14:47 +0800 CST2023-11-14 22:14:47 +0800 CST 2023-11-14 22:14:47 +0800 CST

带有when条件的ansible处理程序

  • 772

所以我有这本剧本

- hosts: localhost
  gather_facts: no
  vars:
    this_thing_is_true: true
    use_handler: false

  tasks:
    - debug:
        msg: 'Notifying handlers'
      changed_when: this_thing_is_true
      notify:
        - me

  handlers:
    - name: me
      debug:
        msg: 'I have been notified'
      when: use_handler is true

当我按预期运行它时,处理程序不会运行。

# ansible-playbook handler.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
skipping: [localhost]

use_handler我可以通过更改剧本中的变量来激活处理程序。

 # ansible-playbook handler.yml  
PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
ok: [localhost] => {
    "msg": "I have been notified"
}

然而,我认为这也会激活处理程序......但事实并非如此。

# ansible-playbook -e use_handler=true handler.yml


PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
skipping: [localhost]

难道我做错了什么?

ansible
  • 2 2 个回答
  • 26 Views

2 个回答

  • Voted
  1. EricHymowitz
    2023-11-15T03:01:43+08:002023-11-15T03:01:43+08:00

    我从另一个论坛得到了答案

    问题在于,在命令行上使用 key=value 样式的额外变量将 bool 传递到运行中是不切实际的。要设置布尔值,您需要使用 JSON。看哪:

    [utoddl@tango ansible]$ ansible localhost -e use_handler=true -m debug -a '{"msg": "{{ use_handler | type_debug }}"}'
    localhost | SUCCESS => {
        "msg": "str"
    }
    [utoddl@tango ansible]$ ansible localhost -e '{"use_handler": true}' -m debug -a '{"msg": "{{ use_handler | type_debug }}"}'
    localhost | SUCCESS => {
        "msg": "bool"
    }
    

    另一种选择是用字符串检查替换处理程序中的布尔检查

    when: use_handler == 'true'
    
    • 0
  2. Best Answer
    Vladimir Botka
    2023-11-15T03:04:13+08:002023-11-15T03:04:13+08:00

    改变条件

          when: use_handler|bool
    
    • 当以选项的 INI 格式声明时,变量use_handler的类型是字符串

    --extra-vars use_handler=true

    如果你愿意的话可以测试一下

      - debug:
          var: use_handler|type_debug
    
    • 您不必将其显式测试为is true. 可以在测试中使用普通布尔变量。
    • 0

相关问题

  • Ansible:将字符串转换为字典

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