gpt4 book ai didi

Ansible忽略条件

转载 作者:行者123 更新时间:2023-12-01 23:54:39 34 4
gpt4 key购买 nike

我正在为我的服务器(ubuntu 和 centos 的混合)开发一个 ansible 剧本,如果安装了一个程序,当我尝试为 monit 传输一些配置文件时,我遇到了一个问题。它在我的 centos 机器上完美运行,但在 ubuntu 机器上,无论完全忽略条件,它都会传输模板。

---
- name: Check for Sendmail (Ubuntu)
shell: dpkg-query -W -f='${Status} ${Version}\n' sendmail
register: ubuntu_installed
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
changed_when: False
failed_when: "'FAILED' in ubuntu_installed.stderr"

- debug: var=ubuntu_installed

- name: Check for Sendmail (CentOs)
shell: rpm -qa | grep sendmail
register: cent_installed
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'
changed_when: False
failed_when: "'FAILED' in cent_installed.stderr"

- name: Transfer Monit config files for Sendmail (Ubuntu)
template: src=monit/templates/sendmail.j2 dest=/etc/monit/conf.d/sendmail owner=root group=root mode=644
when: ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "" and ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

- name: Transfer Monit config files for Sendmail (CentOs)
template: src=monit/templates/sendmail.j2 dest=/etc/monit.d/sendmail owner=root group=root mode=644
when: cent_installed.skipped is not defined and cent_installed.stdout != "" and ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

我在其中进行了调试,以检查变量是否正确。当我运行剧本时,我得到...

TASK: [monit | debug var=ubuntu_installed] ************************************
ok: [server1] => {
"item": "",
"ubuntu_installed": {
"changed": false,
"cmd": "dpkg-query -W -f='${Status} ${Version}\\n' sendmail ",
"delta": "0:00:00.012985",
"end": "2014-07-11 16:56:12.688509",
"failed": false,
"failed_when_result": false,
"invocation": {
"module_args": "dpkg-query -W -f='${Status} ${Version}\\n' sendmail",
"module_name": "shell"
},
"item": "",
"rc": 1,
"start": "2014-07-11 16:56:12.675524",
"stderr": "dpkg-query: no packages found matching sendmail",
"stdout": "",
"stdout_lines": []
}
}

它完全忽略了条件 ubuntu_installed.stdout != ""

最佳答案

这是你的条件:

-  when: ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "" and ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

你的 precedence不正确:A and B and C or D 表示是(A and B and C)或 D,这不是您想要的。 (注意 Ansible conditionals 使用 Jinja2 expressions ,它反射(reflect)了 Python 条件,所以 here are the precedence rules )。

懒惰的程序员为了清楚起见添加括号;这是修改后的条件:

-  when: (ubuntu_installed.skipped is not defined and ubuntu_installed.stdout != "") and (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu')

PS- 你显示了一个命令的输出,debug,但是给 alllll 输出是有帮助的。那是 ansible-playbook -vvv

关于Ansible忽略条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708269/

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