当前位置: 首页 > news >正文

韩雪冬网站网络公司名字大全集

韩雪冬网站,网络公司名字大全集,以绿色为主的网站,在线ui设计平台环境 控制节点:Ubuntu 22.04Ansible 2.10.8管理节点:CentOS 8 block 顾名思义,通过block可以把task按逻辑划分到不同的“块”里面,实现“块操作”。此外,block还提供了错误处理功能。 task分组 下面的例子&#x…

环境

  • 控制节点:Ubuntu 22.04
  • Ansible 2.10.8
  • 管理节点:CentOS 8

block

顾名思义,通过block可以把task按逻辑划分到不同的“块”里面,实现“块操作”。此外,block还提供了错误处理功能。

task分组

下面的例子,把3个task放到一个block里面。

创建文件 testBlock1.yml 如下:

---
- name: testBlock1hosts: alltasks:- name: My task 1block:- name: Part1debug:msg: "Hello Zhang San"- name: Part2debug:msg: "Hello Li Si"- name: Part3debug:msg: "Hello Wang Wu"when: 2 > 1

运行结果如下:

➜  temp ansible-playbook testBlock1.ymlPLAY [testBlock1] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Zhang San"
}TASK [Part2] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Li Si"
}TASK [Part3] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Wang Wu"
}PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

如果把block的判断条件 when: 2 > 1 改为 when: 2 == 1 ,则运行结果如下:

➜  temp ansible-playbook testBlock1.ymlPLAY [testBlock1] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
skipping: [192.168.1.55]TASK [Part2] ***************************************************************************************
skipping: [192.168.1.55]TASK [Part3] ***************************************************************************************
skipping: [192.168.1.55]PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0 

可见,由于条件不满足,block里的3个task都没有执行。

错误处理

如果block里的某个task出错了,则后面的task不再运行。

创建文件 testBlock2.yml 如下:

---
- name: testBlock2hosts: alltasks:- name: My task 1block:- name: Part1debug:msg: "Hello Zhang San"- name: Part2command: /bin/false # will trigger an error- name: Part3debug:msg: "Hello Li Si"

运行结果如下:

➜  temp ansible-playbook testBlock2.ymlPLAY [testBlock2] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Zhang San"
}TASK [Part2] ***************************************************************************************
fatal: [192.168.1.55]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.001904", "end": "2023-10-26 08:50:36.850526", "msg": "non-zero return code", "rc": 1, "start": "2023-10-26 08:50:36.848622", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

可见,由于Part2出错,Part3并没有运行。

Ansible的错误处理有两个关键字:

  • rescue :类似于 catch
  • always :类似于 finally

先加上 always 看看效果:

---
- name: testBlock2hosts: alltasks:- name: My task 1block:- name: Part1debug:msg: "Hello Zhang San"- name: Part2command: /bin/false # will trigger an error- name: Part3debug:msg: "Hello Li Si"always:- name: Always do thisdebug:msg: "End End End"
➜  temp ansible-playbook testBlock2.ymlPLAY [testBlock2] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Zhang San"
}TASK [Part2] ***************************************************************************************
fatal: [192.168.1.55]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002734", "end": "2023-10-26 08:52:19.329781", "msg": "non-zero return code", "rc": 1, "start": "2023-10-26 08:52:19.327047", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}TASK [Always do this] ******************************************************************************
ok: [192.168.1.55] => {"msg": "End End End"
}PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

可见,Part2出错, always 也会运行(当然,Part3不会运行)。

注意:加上 always ,failed仍然是1。

现在来试一下 rescue

---
- name: testBlock2hosts: alltasks:- name: My task 1block:- name: Part1debug:msg: "Hello Zhang San"- name: Part2command: /bin/false # will trigger an error- name: Part3debug:msg: "Hello Li Si"always:- name: Always do thisdebug:msg: "End End End"rescue:- name: Rescue tasksdebug:msg: "Something is wrong!"

运行结果如下:

➜  temp ansible-playbook testBlock2.ymlPLAY [testBlock2] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
ok: [192.168.1.55] => {"msg": "Hello Zhang San"
}TASK [Part2] ***************************************************************************************
fatal: [192.168.1.55]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.001726", "end": "2023-10-26 09:00:01.785445", "msg": "non-zero return code", "rc": 1, "start": "2023-10-26 09:00:01.783719", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}TASK [Rescue tasks] ********************************************************************************
ok: [192.168.1.55] => {"msg": "Something is wrong!"
}TASK [Always do this] ******************************************************************************
ok: [192.168.1.55] => {"msg": "End End End"
}PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0  

注意,always是在rescue之后运行的(Part3仍然不会运行)。

注:如果task没有出错, rescue 不会被触发。

rescue与handler

我们知道,当task运行成功,状态改变时,可以用 notify 来触发handler。但如果后续的task出错了,则当前task的handler并不会触发。如果有 rescue ,则handler仍然会被触发(在 always 之后)。在 rescue 中可以通过 meta: flush_handlers 来立即触发handler(在 always 之前)。

---
- name: testBlock2hosts: alltasks:- name: My task 1block:- name: Part1debug:msg: "Hello Zhang San"changed_when: truenotify: Run me even after an error- name: Part2command: /bin/false # will trigger an error- name: Part3debug:msg: "Hello Li Si"always:- name: Always do thisdebug:msg: "End End End"rescue:- name: Rescue tasks#debug:#  msg: "Something is wrong!"meta: flush_handlershandlers:- name: Run me even after an errordebug:msg: 'This handler runs even on error'

运行结果如下:

➜  temp ansible-playbook testBlock2.ymlPLAY [testBlock2] **********************************************************************************TASK [Gathering Facts] *****************************************************************************
ok: [192.168.1.55]TASK [Part1] ***************************************************************************************
changed: [192.168.1.55] => {"msg": "Hello Zhang San"
}TASK [Part2] ***************************************************************************************
fatal: [192.168.1.55]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002172", "end": "2023-10-26 09:11:26.530609", "msg": "non-zero return code", "rc": 1, "start": "2023-10-26 09:11:26.528437", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}RUNNING HANDLER [Run me even after an error] *******************************************************
ok: [192.168.1.55] => {"msg": "This handler runs even on error"
}TASK [Always do this] ******************************************************************************
ok: [192.168.1.55] => {"msg": "End End End"
}PLAY RECAP *****************************************************************************************
192.168.1.55               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0 

本例中,Part1运行成功,且改变了状态,所以触发了handler Run me even after an error 。但由于Part2出错,如果没有 rescue ,则Part1的handler不会触发。加上 rescue 之后,就会触发Part1的handler。本例中加上了 meta: flush_handlers ,所以会立即触发handler。

参考

  • https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html
http://www.yayakq.cn/news/748587/

相关文章:

  • 建设安全工程信息网站网站建设官网型好呢还是商城型
  • wordpress前台发布南通网站推广优化费用
  • 如何使用wordpress建站网站开发开题报告
  • 网站构架图怎么做wordpress反复输入密码
  • 公众号建网站房地产建设网站的意义
  • 做网站实验报告网站建设培训公司哪家好
  • 江西建设职业技能教育咨询网站网站开发毕业设计书
  • php网站制作实例教程2019做地方门户网站
  • wordpress建站被黑深圳正规融资公司
  • 响应式网站导航栏模板学校特色网站建设情况
  • 现在建设一个基础的网站多少钱网站开发包含什么
  • 重庆中国建设银行招聘信息网站京东网上商城首页
  • 昆明网站制作专业网络推广的概念
  • asp.net网站项目建设wordpress控制台
  • google如何提交网站wordpress美化文章内相册
  • 百度网站优化外包轮播网站
  • 在线考试类网站怎么做网站建设公司石家庄
  • 大气蓝色wap网站模板中国最大的外贸平台
  • 该网站正在建设2015做那些网站致富
  • 网站建设一般要多大空间网站上传不了wordpress
  • 福州市城乡建设局网站好的响应式网站有哪些
  • 商务网站创建经费预算app网站开发协议
  • 中国手机网站大全推广网站刷排名
  • 凡科建站的建站后如何管理宁波网站设计制作
  • 直播网站开发核心技术成都公司注册流程及需要的材料
  • 郑州网站推广流程wordpress邮箱163
  • 如何做盗版视频网站优化设计六年级下册数学答案
  • 淮安网站建设公司电话网站推广公司兴田德润在哪儿
  • swoole怎么做直播网站温州seo推广外包
  • 公司怎么建网站做推广自己建一个网站做电子商务