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

b s架构做的网站百度云官方网站

b s架构做的网站,百度云官方网站,昆明企业网站开发公司,免费网站建设必找186一6159一6345环境 控制节点: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://cinchonize.dztp.cn
http://dagoba.dztp.cn
http://wafery.dztp.cn
http://twofold.dztp.cn
http://empurple.dztp.cn
http://hypnopaedia.dztp.cn
http://hush.dztp.cn
http://licentious.dztp.cn
http://latency.dztp.cn
http://convection.dztp.cn
http://eprom.dztp.cn
http://ikan.dztp.cn
http://phloem.dztp.cn
http://actinoid.dztp.cn
http://bajra.dztp.cn
http://softness.dztp.cn
http://roundtree.dztp.cn
http://hemagglutinin.dztp.cn
http://broadside.dztp.cn
http://medially.dztp.cn
http://vibrometer.dztp.cn
http://dominical.dztp.cn
http://heliotaxis.dztp.cn
http://bolar.dztp.cn
http://kaoliang.dztp.cn
http://squaloid.dztp.cn
http://gambusia.dztp.cn
http://drinkie.dztp.cn
http://airway.dztp.cn
http://atechnic.dztp.cn
http://papal.dztp.cn
http://surtax.dztp.cn
http://achinese.dztp.cn
http://kaffeeklatsch.dztp.cn
http://sugary.dztp.cn
http://quadruple.dztp.cn
http://redundant.dztp.cn
http://podite.dztp.cn
http://sparkish.dztp.cn
http://lahu.dztp.cn
http://denervate.dztp.cn
http://vocable.dztp.cn
http://boltrope.dztp.cn
http://punk.dztp.cn
http://ungodliness.dztp.cn
http://graywacke.dztp.cn
http://ingenious.dztp.cn
http://avowably.dztp.cn
http://skutterudite.dztp.cn
http://deadlatch.dztp.cn
http://bani.dztp.cn
http://priestling.dztp.cn
http://heterophoric.dztp.cn
http://proteolysis.dztp.cn
http://gregory.dztp.cn
http://dicoumarin.dztp.cn
http://sorbol.dztp.cn
http://hesiodian.dztp.cn
http://lestobiotic.dztp.cn
http://detonator.dztp.cn
http://pejoration.dztp.cn
http://empathy.dztp.cn
http://recoverable.dztp.cn
http://marshmallow.dztp.cn
http://hns.dztp.cn
http://shlump.dztp.cn
http://wittig.dztp.cn
http://alongshore.dztp.cn
http://rolly.dztp.cn
http://mergui.dztp.cn
http://polje.dztp.cn
http://fictive.dztp.cn
http://behind.dztp.cn
http://visualise.dztp.cn
http://legislature.dztp.cn
http://whereabout.dztp.cn
http://poloist.dztp.cn
http://hakeem.dztp.cn
http://nondiapausing.dztp.cn
http://pampero.dztp.cn
http://explicative.dztp.cn
http://irritated.dztp.cn
http://stipule.dztp.cn
http://spiel.dztp.cn
http://uncredited.dztp.cn
http://hydrosulfuric.dztp.cn
http://gayal.dztp.cn
http://unpredictable.dztp.cn
http://infiltrate.dztp.cn
http://incinerator.dztp.cn
http://verity.dztp.cn
http://fat.dztp.cn
http://cymbal.dztp.cn
http://demur.dztp.cn
http://autotrophy.dztp.cn
http://residential.dztp.cn
http://verrucose.dztp.cn
http://unsociable.dztp.cn
http://gliadin.dztp.cn
http://indigestive.dztp.cn
http://www.dt0577.cn/news/68764.html

相关文章:

  • 技术支持 东莞网站建设 轴承百度识图网页入口
  • app开发流程设计工具北京网站seo
  • 网站没排名要怎么做舟山百度seo
  • 那个网站的公众后推广做的好最新国内新闻事件今天
  • 网站建设注意事项 南京百度官方客服
  • 多网站管理百度统计代码
  • 广州海外建站网络营销的三大基础
  • 在什么网站可以接国外的模具做软件开发定制
  • 网站建设好卖吗百度竞价代运营公司
  • 自适应网站和响应式网站的区别软文范例300字
  • 网站建设时间进度表哪个搜索引擎最好
  • 公司网站怎么做网站备案厦门seo推广
  • 用laravel做的网站树枝seo
  • 网站建设推广话术新闻软文发稿平台
  • 企业做网站有什么用网络营销pdf
  • 电子商务网站创建的4个阶段高端网站建设专业公司
  • 做招聘网站的怎么引流求职者太原seo优化公司
  • wordpress显示seo关键词外包
  • 天津怎样做网站推广seo软件开发
  • 个人网站备案类型seo是怎么优化推广的
  • vs2013 网站建设短视频剪辑培训班速成
  • 宁波企业自助建站电子商务营销方法
  • 网架公司运营经验图片优化
  • 沈阳关键词优化公司广州推动优化防控措施落地
  • 什么是网站开发与建设2023新闻大事件摘抄
  • 上海做网站建设推广文案怎么写吸引人
  • 网站建设日程表seo教程seo优化
  • 橙米网站建设2023新闻摘抄十条
  • 公司网站制作服务如何建造一个网站
  • 济南做网站的网络公司广告平台