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

网站的彩色标签怎么做的聊城网页设计公司

网站的彩色标签怎么做的,聊城网页设计公司,关键词挖掘长尾词,长沙网站seo收费环境 控制节点: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/551387/

相关文章:

  • 用eclipce做登录网站做电脑系统哪个网站
  • 怎么创建免费的网站贵阳花溪建设村镇银行官方网站
  • 网站关键词制作石家庄做网站时光
  • 做自媒体的网站做网站江西
  • 朝阳港网站建设方案临汾做网站
  • 网站建设开发五行属性昆明云南微网站制作哪家好
  • 网站建设微信商城开发哪个公司建网站最好
  • 企业建设网站是网络营销吗做网站送的企业邮箱能用吗
  • 前端工程师是做网站吗wordpress无法目录下
  • 编写网站的软件网页怎么注册
  • 色卡网站手机网站Com
  • 网站安全防护方案设计制作一个保温杯
  • 做带v头像的网站百度网址大全网站
  • 什么网站ppt做的最好看建盏公司哪几家
  • 4a级景区网站建设年前做网站的好处
  • 用html编写淘宝网站怎么做深圳网络推广哪家
  • 无极网站网站中国十大平面设计师
  • 深圳赶集同城网站建设肇庆市建设企业网站怎么样
  • 做微商能利用的网站有哪些怎么制作链接
  • 管网建设网站江苏智能网站建设流程
  • 广州个性化网站开发竞价推广方案怎么写
  • 网站开发赚钱的方法宣传片拍摄合同模板
  • 爱网站找不到了沈阳网页设计专业
  • 电子商务网站开发与建设网站模块设计软件
  • 有没有可以发布需求的网站专业做家政网站
  • 精品成品网站1688一家做运动鞋的网站
  • 网站颜色表做网站需要什么图片
  • 免费php域名网站高端网站建设多少钱
  • 四川省城乡建设信息网站证件查询网站全局参数设置
  • 聊城专业网站建设flash个人网站源码