- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
[ansible@master ansible]$ vim when-rhel.yaml
- name: install vim
hosts: test
tasks:
- name: install vim via yum
yum:
name: vim
state: present
when: ansible_os_family == "RedHat"
ansible_os_family 这个变量可以在setup收集的信息里面找到 。
为什么需要用到判断呢?
因为只有在红帽系的发行版中,包管理工具使用的是yum,而在debian的系列中,使用的apt,所以如果你管理的主机有红帽系的,也有debian系的,那么就需要写一个判断来让他们分开执行 。
[ansible@master ansible]$ ansible-playbook when-rhel.yaml
TASK [install vim via yum] *****************************************************
ok: [192.168.200.200]
ok: [192.168.200.210]
PLAY RECAP *********************************************************************
192.168.200.200 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.200.210 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
通过这个回显我们可以得知,任务他是要执行的,但是由于我们的系统上有vim,所以他没有发生改变,显示的是ok 。
那我们将ansible_os_family == "RedHat" 改为Debian看看呢 。
- name: install vim
hosts: test
tasks:
- name: install vim via yum
yum:
name: vim
state: present
when: ansible_os_family == "Debain"
执行剧本 。
[ansible@master ansible]$ ansible-playbook when-rhel.yaml
PLAY [install vim] *************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.200]
ok: [192.168.200.210]
TASK [install vim via yum] *****************************************************
skipping: [192.168.200.210]
skipping: [192.168.200.200]
PLAY RECAP *********************************************************************
192.168.200.200 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.200.210 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
可以看到,他的状态是skipping,也就说跳过的意思,因为他没有跟条件语句匹配上,所以他不会执行 。
在ansible中,支持以下比较运算符 。
假设现在的场景是这样的,操作系统刚装完,现在需要你对这些系统进行一些初始化的操作,比如安装一些必要的软件包,关闭防火墙,selinux,但是现在有一个问题,就是这些操作系统并不一定都是红帽,可能还有Debian,Debian上面的操作跟红帽的操作就又不一样了啊 。
如果我们按照之前的思维,那么就是安装软件包的时候有一个判断语句,关闭selinux又是一个判断语句,这样下来我们就需要写很多一模一样的判断语句了,能不能只写一条,这条判断语句过了之后直接执行对应的一系列任务呢? 是可以的 。
[ansible@master ansible]$ vim block.yaml
- name: block
hosts: test
tasks:
- block:
- name: install software
yum:
name: vim
state: present
- name: disable Selinux
selinux:
policy: targeted
state: disabled
- name: disabled firewalld
systemd:
name: firewalld
state: stopped
enabled: no
when: ansible_os_family == "RedHat"
- block:
- name: install software
apt:
name: vim
state: present
when: ansible_os_family == "Debian"
通过加上block之后,在block里面定义的任务就是一个整体了,那么条件判断也是针对这个block的,如果条件判断返回为真之后,那么这个block里面的所有任务都会被执行 。
block除了能和when一起使用之外,还可以和rescue一起使用,作用是能做错误处理 。
[ansible@master ansible]$ vim rescue.yaml
- name: rescue
hosts: test
tasks:
- block:
- name: test file exist
shell: "ls /aaa.txt"
rescue:
- name: print file is not exist
debug:
msg: 'file is not exist'
我们的文件 /aaa.txt是不存在的,所以他执行这个任务会报错,但是正常情况下,举报报错就会停止执行了,后面的任务也就不会有输出了,rescue就是来做错误处理的,如果这里面有错误的话,他不会影响剧本执行,反而还会触发resue字段里的任务 。
[ansible@master ansible]$ ansible-playbook rescue.yaml
PLAY [rescue] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.200]
ok: [192.168.200.210]
TASK [test file exist] *********************************************************
fatal: [192.168.200.210]: FAILED! => {"changed": true, "cmd": "ls /aaa.txt", "delta": "0:00:00.002396", "end": "2024-06-21 10:20:25.001243", "msg": "non-zero return code", "rc": 2, "start": "2024-06-21 10:20:24.998847", "stderr": "ls: cannot access '/aaa.txt': No such file or directory", "stderr_lines": ["ls: cannot access '/aaa.txt': No such file or directory"], "stdout": "", "stdout_lines": []}
fatal: [192.168.200.200]: FAILED! => {"changed": true, "cmd": "ls /aaa.txt", "delta": "0:00:01.003922", "end": "2024-06-21 10:20:25.992768", "msg": "non-zero return code", "rc": 2, "start": "2024-06-21 10:20:24.988846", "stderr": "ls: cannot access '/aaa.txt': No such file or directory", "stderr_lines": ["ls: cannot access '/aaa.txt': No such file or directory"], "stdout": "", "stdout_lines": []}
TASK [print file is not exist] *************************************************
ok: [192.168.200.210] => {
"msg": "file is not exist"
}
ok: [192.168.200.200] => {
"msg": "file is not exist"
}
PLAY RECAP *********************************************************************
192.168.200.200 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
192.168.200.210 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0
我们可以看到,执行命令报错了,但是任务剧本没有被打断,后面还输出了file is not exist 。
那我们来将文件改为存在的,看看rescue还会执行吗 。
[ansible@master ansible]$ vim rescue.yaml
- name: rescue
hosts: test
tasks:
- block:
- name: test file exist
shell: "ls /etc/passwd"
rescue:
- name: print file is not exist
debug:
msg: 'file is not exist'
执行这个剧本 。
[ansible@master ansible]$ ansible-playbook rescue.yaml
[ansible@master ansible]$ ansible-playbook rescue.yaml
PLAY [rescue] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.210]
ok: [192.168.200.200]
TASK [test file exist] *********************************************************
changed: [192.168.200.200]
changed: [192.168.200.210]
PLAY RECAP *********************************************************************
192.168.200.200 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.200.210 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
我们可以看到,这个任务执行成功了,他没有输出rescue里面定义的任务 。
也就是说,只有block里面的任务出错的时候rescue才会接管任务,平时是不会执行的 。
当block执行失败,rescue才会执行;而无论block失败还是成功,always里面的任务都将会执行 。
- name: always
hosts: test
tasks:
- block:
- name: success
debug:
msg: "this is success task"
- name: faild
debug:
msg: "{{ aaa }}"
rescue:
- name: print faile
debug:
msg: "Task failure"
always:
- name: print always
debug:
msg: "This task is always performed"
这个剧本定义了一个成功的任务,一个失败的任务,当有任务失败的时候会触发rescue,当所以任务执行完之后会执行always 。
[ansible@master ansible]$ ansible-playbook always.yaml
[ansible@master ansible]$ ansible-playbook always.yaml
PLAY [always] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.210]
ok: [192.168.200.200]
TASK [success] *****************************************************************
ok: [192.168.200.210] => {
"msg": "this is success task"
}
ok: [192.168.200.200] => {
"msg": "this is success task"
}
TASK [faild] *******************************************************************
fatal: [192.168.200.210]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'aaa' is undefined\n\nThe error appears to be in '/home/ansible/ansible/always.yaml': line 8, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"this is success task\"\n - name: faild\n ^ here\n"}
fatal: [192.168.200.200]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'aaa' is undefined\n\nThe error appears to be in '/home/ansible/ansible/always.yaml': line 8, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"this is success task\"\n - name: faild\n ^ here\n"}
TASK [print faile] *************************************************************
ok: [192.168.200.210] => {
"msg": "Task failure"
}
ok: [192.168.200.200] => {
"msg": "Task failure"
}
TASK [print always] ************************************************************
ok: [192.168.200.210] => {
"msg": "This task is always performed"
}
ok: [192.168.200.200] => {
"msg": "This task is always performed"
}
最后此篇关于Playbook条件语句的文章就讲到这里了,如果你想了解更多关于Playbook条件语句的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“else”代码时,它给了我一
With 语句 对一个对象执行一系列的语句。 With object statements End With 参数 object 必需的部分
While...Wend 语句 当指定的条件为 True 时,执行一系列的语句。 While condition  ; Version [stat
所以我正在处理的代码有一个小问题。 while True: r = input("Line: ") n = r.split() if r == " ":
我有一个对象数组: var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number"
int main() { int f=fun(); ... } int fun() { return 1; return 2; } 在上面的程序中,当从main函数中调用一个
我的项目中有很多 if 语句、嵌套 if 语句和 if-else 语句,我正在考虑将它们更改为 switch 语句。其中一些将具有嵌套的 switch 语句。我知道就编译而言,switch 语句通常更
Rem 语句 包含程序中的解释性注释。 Rem comment 或 ' comment comment 参数是需要包含的注释文本。在 Rem 关键字和 comment 之间应有一个空格。
ReDim 语句 在过程级中声明动态数组变量并分配或重新分配存储空间。 ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Randomize 语句 初始化随机数生成器。 Randomize [number] number 参数可以是任何有效的数值表达式。 说明 Randomize 使用 number 参数初始
Public 语句 定义公有变量并分配存储空间。在 Class 块中定义私有变量。 Public varname[([subscripts])][, varname[([subscripts])
Sub 语句 声明 Sub 过程的名称、参数以及构成其主体的代码。 [Public [Default]| Private] Sub name [( arglist )]
Set 语句 将对象引用赋给一个variable或property,或者将对象引用与事件关联。 Set objectvar = {objectexpression | New classname
我有这个代码块,有时第一个 if 语句先运行,有时第二个 if 语句先运行。我不确定为什么会这样,因为我认为 javascript 是同步的。 for (let i = 0; i < dataObje
这是一个 javascript 代码,我想把它写成这样:如果此人回答是,则回复“那很酷”,如果此人回答否,则回复“我会让你开心”,如果此人回答的问题包含"is"或“否”,请说“仅键入”是或否,没有任何
这是我的任务,我尝试仅使用简短的 if 语句来完成此任务,我得到的唯一错误是使用“(0.5<=ratio<2 )”,除此之外,构造正确吗? Scanner scn = new Scanner(
有没有办法在 select 语句中使用 if 语句? 我不能在这个中使用 Case 语句。实际上我正在使用 iReport 并且我有一个参数。我想要做的是,如果用户没有输入某个参数,它将选择所有实例。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: If vs. Switch Speed 我将以 C++ 为例,但我要问的问题不是针对特定语言的。我的意思是一
Property Set 语句 在 Class 块中,声明名称、参数和代码,这些构成了将引用设置到对象的 Property 过程的主体。 [Public | Private] Pro
Property Let 语句 在 Class 块中,声明名称、参数和代码等,它们构成了赋值(设置)的 Property 过程的主体。 [Public | Private] Prop
我是一名优秀的程序员,十分优秀!