中的调试语句旁边): - debug: var=nrpe_installed.stat.exists - debug: var=force_in-6ren">
gpt4 book ai didi

Ansible "when variable == true"未按预期运行

转载 作者:行者123 更新时间:2023-12-02 08:25:51 25 4
gpt4 key购买 nike

我正在编写的剧本中有以下任务(结果列在 <> 中的调试语句旁边):

  - debug: var=nrpe_installed.stat.exists <true>
- debug: var=force_install <true>
- debug: var=plugins_installed.stat.exists <true>

- name: Run the prep
include: prep.yml
when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)
tags: ['prep']

- debug: var=nrpe_installed.stat.exists <true>
- debug: var=force_install <true>
- debug: var=force_nrpe_install <false>

- name: Install NRPE
include: install-nrpe.yml
when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)
tags: ['install_nrpe']
vars:
nrpe_url: 'http://url.goes.here'
nrpe_md5: 3921ddc598312983f604541784b35a50
nrpe_version: 2.15
nrpe_artifact: nrpe-{{ nrpe_version }}.tar.gz
nagios_ip: {{ nagios_ip }}
config_dir: /home/ansible/config/

我使用以下命令运行它:

ansible-playbook install.yml -i $invFile --extra-vars="hosts=webservers force_install=True"

第一个包含运行,但第二个包含以下输出:

 skipping: [server1] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

我的印象是,条件检查应该对所有这些都通过,因为 force_install == true 计算结果为 true 这应该使整个 when 计算结果为 true(因为它是一系列“OR”)。

当变量设置正确时,如何获取运行时间?

<小时/>

编辑:

安装 NRPE 的第二个时间更改为以下内容,但没有解释为什么另一个 运行准备 正确运行:

工作:

when: (not nrpe_installed.stat.exists or force_install or force_nrpe_install)

还在工作:

when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true)

不工作:

when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true)
<小时/>

该剧特定部分的截断(删除重复项)输出为:

TASK [debug] *******************************************************************
ok: [server2] => {
"nrpe_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
"plugins_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
"force_install": true
}

TASK [Run the prep] ************************************************************
included: /tasks/nrpe-install/prep.yml for server2, server3, server4, server5, server6, server7

TASK [Prep and configure for installation | Install yum packages] **************
ok: [server6] => (item=[u'gcc', u'glibc', u'glibc-common', u'gd', u'gd-devel', u'make', u'net-snmp', u'openssl-devel', u'unzip', u'tar', u'gzip', u'xinetd']) => {"changed": false, "item": ["gcc", "glibc", "glibc-common", "gd", "gd-devel", "make", "net-snmp", "openssl-devel", "unzip", "tar", "gzip", "xinetd"], "msg": "", "rc": 0, "results": ["gcc-4.1.2-55.el5.x86_64 providing gcc is already installed", "glibc-2.5-123.el5_11.3.i686 providing glibc is already installed", "glibc-common-2.5-123.el5_11.3.x86_64 providing glibc-common is already installed", "gd-2.0.33-9.4.el5_4.2.x86_64 providing gd is already installed", "gd-devel-2.0.33-9.4.el5_4.2.i386 providing gd-devel is already installed", "make-3.81-3.el5.x86_64 providing make is already installed", "net-snmp-5.3.2.2-20.el5.x86_64 providing net-snmp is already installed", "openssl-devel-0.9.8e-40.el5_11.x86_64 providing openssl-devel is already installed", "unzip-5.52-3.el5.x86_64 providing unzip is already installed", "tar-1.15.1-32.el5_8.x86_64 providing tar is already installed", "gzip-1.3.5-13.el5.centos.x86_64 providing gzip is already installed", "xinetd-2.3.14-20.el5_10.x86_64 providing xinetd is already installed"]}

TASK [Prep and configure for installation | Make nagios group] *****************
ok: [server2] => {"changed": false, "gid": 20002, "name": "nagios", "state": "present", "system": false}

TASK [Prep and configure for installation | Make nagios user] ******************
ok: [server6] => {"append": false, "changed": false, "comment": "User for Nagios NRPE", "group": 20002, "home": "/home/nagios", "move_home": false, "name": "nagios", "shell": "/bin/bash", "state": "present", "uid": 20002}

TASK [debug] *******************************************************************
ok: [server2] => {
"nrpe_installed.stat.exists": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
"force_install": true
}

TASK [debug] *******************************************************************
ok: [server2] => {
"force_nrpe_install": false
}

TASK [Install NRPE] ************************************************************
skipping: [server2] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

最佳答案

您需要将变量转换为 bool 值:

force_install|bool == true

我并不声称我理解其背后的逻辑。在 python 中,任何非空字符串都应该是真实的。但当直接在条件中使用时,它的计算结果为 false。

bool 过滤器再次解释字符串 'yes''on''1''true' (不区分大小写)和 1 为 true(请参阅 source )。任何其他字符串都是 false。

您可能还想设置一个默认值,以防 force_install 未定义,因为这会导致 undefined variable 错误:

force_install|default(false)|bool == true

关于Ansible "when variable == true"未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com