gpt4 book ai didi

Ansible:when子句中的多个和/或条件

转载 作者:行者123 更新时间:2023-12-01 07:24:11 25 4
gpt4 key购买 nike

我在尝试在when语句中使用多个和/或条件语句来确定是否需要运行任务时遇到问题。基本上,我正在编写一本剧本来进行自动系统补丁,其中包括安全补丁,仅内核补丁以及在var文件中指定软件包的选项。

我使用以下命令运行剧本,并通过扩展变量选项(-e)定义变量
ansible-playbook site.yml -i inventory --ask-vault -u (username) -e "security=true restart=true" -k -K

默认情况下,剧本会更新系统上除内核以外的所有软件包,但是如果我指定了一些变量中的任何一个,我想跳过该操作。我的代码如下:

- name: Update all packages
yum:
name: "*"
state: latest
exclude: "kernel*"
when: security is not defined or kernel is not defined or specified_packages
is not defined and ansible_os_family == "RedHat"

香港专业教育学院尝试了以下所有组合:
when: (ansible_os_family == "RedHat") and (security is defined or kernel is defined or specified_packages is defined) when: (ansible_os_family == "RedHat") and (security == true or kernel == true or specified_packages == true ) <-这种情况会引发未定义的错误,因为我每次运行剧本时都没有定义所有变量
when: ansible_os_family == "RedHat"
when: security is defined or kernel is defined or specified_packages is defined

注意:我知道并使用了诸如“skip”之类的额外变量来跳过此任务,并使用when子句 when: ansible_os_family == "RedHat" and skip is not defined,但不想让我的用户仅需跳过此默认操作就需要使用额外的变量。

我也没有使用标签,因为我在升级前后收集软件包列表以进行比较并最终报告,因此我将无法运行这些软件包,因为它们是本地操作命令。这就是为什么我使用一个角色通过扩展变量来打开和关闭多个任务的原因。我乐于接受任何建议,因为我有点菜鸟,所以可以以更有效的方式重写剧本。

最佳答案

这是一个简单的答案!

以下作品:

when: not (security is defined or kernel is defined or specified_packages is defined) and ansible_os_family == "RedHat"

关于Ansible:when子句中的多个和/或条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44838421/

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