ansible自动化运维(5)

本文介绍了如何在Ansible中处理任务失败,包括忽略错误、强制执行处理程序以及使用`failed_when`和`changed_when`关键字控制任务状态。此外,还详细讲解了文件模块的使用,如`blockinfile`, `copy`, `fetch`, `file`, `lineinfile`等,并展示了如何使用Jinja2模板动态构建和部署文件。文章最后探讨了管理大型项目时的主机选择、动态清单、并行配置、滚动更新以及包含和导入文件的策略。" 125499998,11446017,今日头条小程序:新特性与未来发展趋势,"['小程序', '微信小程序', '内容电商', '支付', 'Android']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##处理任务失败

通常playbook遇到错误会中止执行,但是有时我们想要失败时也继续执行

##忽略任务失败
关键字:ignore_errors
#举例
[root@workstationcode]#cat ignore.yml

---
- name: Test
  hosts: webservers
  tasks:
    - name: Install package
      yum:
        name: k8s
        state: latest
      ignore_errors: yes


#执行

[root@workstationcode]#ansible-playbook ignore.yml
...
TASK[Installpackage]*********************************************************
fatal:[servera.lab.example.com]:FAILED!=>{"changed":false,"failures":["No
packagek8savailable."],"msg":"Failedtoinstallsomeofthespecifiedpackages",
"rc":1,"results":[]}
...ignoring

##任务失败后强制执行处理程序
通常任务失败,play会中止,那么收到play中之前任务通知的处理程序将不会运行,如果要运
行,需要使用关键字:force_handlers: yes
#举例
[root@workstationcode]#cat force.yml

---
- hosts: webservers
  force_handlers: yes
  tasks:
    - name: always notify
      command: /bin/true
      notify: restart apache
    - name: Fail task
      yum:
        name: k8s
        state: latest
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

'处理程序会在任务报告changed结果时获得通知,ok或者failed都不会'
##指定任务失败条件
关键字:failed_when

tasks:
  - name: Run Script
    shell: /usr/local/bin/user.sh
    register: command_result
    failed_when: "'failure'incommand_result.stdout"

#fail模块可以实现此效果

tasks:
  - name: Run Script
    shell: /usr/local/bin/user.sh
    register:command_result
    ignore_error: yes
  - name: Report failure
    fail:
      msg: "Authenticationfailure"            #fail模块可以提供明确消息
    when: "'failure'incommand_result.stdout"

##指定任务何时报告"Changed"结果
关键字:changed_when

#
- name: get time
  shell: date
  changed_when: false

##ansible块和错误处理
三种关键字:

block:定义要运行的主要任务
rescue:定义要在block子句中定义的任务失败时运行的任务
always:定义时中独立运行的任务


#练习:
故意制造错误

[root@workstationcode]#cat error.yml

---
- name: Task Failure
  hosts: webservers
  vars:
    web_pkg: http
    db_pkg: mariadb-server
    db_service: mariadb
  tasks:
    - name: Install {
  { web_pkg }} packages
      yum:
        name: "{
  { web_pkg }}"
        state: present
    - name: Install {
  { db_pkg }} packages
      yum:
        name: "{
  { db_pkg }}"
        state: present

运行报错
[root@workstationcode]#ansible-playbook error.yml

...
TASK[Installhttppackages]***************************************************
fatal:[servera.lab.example.com]:FAILED!=>{"changed":false,"failures":["No
packagehttpavailable."],"msg":"Failedtoinstallsomeofthespecifiedpackages",
"rc":1,"results":[]}

第一个任务失败,第二个任务不运行
#添加忽略关键字
[root@workstationcode]#vim error.yml
...
13ignore_errors:yes

---
- name: Task Failure
  hosts: webservers
  vars:
    web_pkg: http
    db_pkg: mariadb-server
    db_service: mariadb
  tasks:
    - name: Install {
  { web_pkg }} packages
      yum:
        name: "{
  { web_pkg }}"
        state: present
      ignore_errors: yes
    - name: Install {
  { db_pkg }} packages
      yum:
        name: "{
  { db_pkg }}"
        state: present

#使用block、rescue、always将任务分开
[root@workstationcode]#cat error.yml

---
- name: Task Failure
  hosts: webservers
  vars:
    web_pkg: http
    db_pkg: mariadb-server
    db_service: mariadb
  tasks:
    - name: Setup Web
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值