块
块创建逻辑任务组。块还提供处理任务错误的方法,类似于许多编程语言中的异常处理。
使用块分组任务
块中的所有任务都继承在块级别应用的指令。除了循环之外,您可以在单个任务上应用的大多数内容都可以在块级别应用,因此块使得设置对任务通用的数据或指令变得容易得多。指令不会影响块本身,它只会被块中的任务继承。例如,when 语句应用于块内的任务,而不是块本身。
tasks:
- name: Install, configure, and start Apache
block:
- name: Install httpd and memcached
ansible.builtin.yum:
name:
- httpd
- memcached
state: present
- name: Apply the foo config template
ansible.builtin.template:
src: templates/src.j2
dest: /etc/foo.conf
- name: Start service bar and enable it
ansible.builtin.service:
name: bar
state: started
enabled: True
when: ansible_facts['distribution'] == 'CentOS'
become: true
become_user: root
ignore_errors: true
在上面的示例中,在 Ansible 运行块中的三个任务之前,将评估 “when” 条件。所有三个任务都继承特权提升指令,以 root 用户身份运行。最后,ignore_errors: true
确保即使某些任务失败,Ansible 也会继续执行剧本。
注意
块中的所有任务(包括通过 include_role 包含的任务)都继承在块级别应用的指令。
块名称自 Ansible 2.3 起可用。我们建议在所有任务中(在块内或其他地方)使用名称,以便在运行剧本时更好地了解正在执行的任务。
使用块处理错误
您可以使用带 rescue
和 always
部分的块来控制 Ansible 对任务错误的响应。
救援块指定在块中较早的任务失败时要运行的任务。这种方法类似于许多编程语言中的异常处理。Ansible 仅在任务返回 “failed” 状态后才运行救援块。错误的任务定义和不可达主机不会触发救援块。
tasks:
- name: Handle the error
block:
- name: Print a message
ansible.builtin.debug:
msg: 'I execute normally'
- name: Force a failure
ansible.builtin.command: /bin/false
- name: Never print this
ansible.builtin.debug:
msg: 'I never execute, due to the above task failing, :-('
rescue:
- name: Print when errors
ansible.builtin.debug:
msg: 'I caught an error, can do stuff here to fix it, :-)'
您还可以向块添加 always
部分。always
部分中的任务无论先前块的任务状态如何都会运行。
tasks:
- name: Always do X
block:
- name: Print a message
ansible.builtin.debug:
msg: 'I execute normally'
- name: Force a failure
ansible.builtin.command: /bin/false
- name: Never print this
ansible.builtin.debug:
msg: 'I never execute :-('
always:
- name: Always do this
ansible.builtin.debug:
msg: "This always executes, :-)"
这些元素一起提供复杂的错误处理。
tasks:
- name: Attempt and graceful roll back demo
block:
- name: Print a message
ansible.builtin.debug:
msg: 'I execute normally'
- name: Force a failure
ansible.builtin.command: /bin/false
- name: Never print this
ansible.builtin.debug:
msg: 'I never execute, due to the above task failing, :-('
rescue:
- name: Print when errors
ansible.builtin.debug:
msg: 'I caught an error'
- name: Force a failure in middle of recovery! >:-)
ansible.builtin.command: /bin/false
- name: Never print this
ansible.builtin.debug:
msg: 'I also never execute :-('
always:
- name: Always do this
ansible.builtin.debug:
msg: "This always executes"
block
中的任务正常执行。如果块中的任何任务返回 failed
,则 rescue
部分执行任务以从错误中恢复。always
部分无论 block
和 rescue
部分的结果如何都会运行。
如果块中发生错误并且救援任务成功,Ansible 会为运行恢复原始任务的失败状态,并继续运行剧本,就好像原始任务已成功一样。已救援的任务被视为成功,不会触发 max_fail_percentage
或 any_errors_fatal
配置。但是,Ansible 仍然会在剧本统计信息中报告失败。
您可以将块与 flush_handlers
一起使用在救援任务中,以确保即使发生错误也会运行所有处理程序
tasks:
- name: Attempt and graceful roll back demo
block:
- name: Print a message
ansible.builtin.debug:
msg: 'I execute normally'
changed_when: true
notify: Run me even after an error
- name: Force a failure
ansible.builtin.command: /bin/false
rescue:
- name: Make sure all handlers run
meta: flush_handlers
handlers:
- name: Run me even after an error
ansible.builtin.debug:
msg: 'This handler runs even on error'
版本 2.1 中的新功能。
Ansible 为 rescue
部分中的任务提供了几个变量
- ansible_failed_task
返回 “failed” 并触发救援的任务。例如,要获取名称,请使用
ansible_failed_task.name
。- ansible_failed_result
触发救援的失败任务的捕获返回结果。这等同于在
register
关键字中使用此变量。
这些可以在 rescue
部分中进行检查
tasks:
- name: Attempt and graceful roll back demo
block:
- name: Do Something
ansible.builtin.shell: grep $(whoami) /etc/hosts
- name: Force a failure, if previous one succeeds
ansible.builtin.command: /bin/false
rescue:
- name: All is good if the first task failed
when: ansible_failed_task.name == 'Do Something'
ansible.builtin.debug:
msg: All is good, ignore error as grep could not find 'me' in hosts
- name: All is good if the second task failed
when: "'/bin/false' in ansible_failed_result.cmd | d([])"
ansible.builtin.fail:
msg: It is still false!!!
注意
在 ansible-core
2.14 或更高版本中,当嵌套块时,两个变量都从内部块传播到外部块的 rescue
部分。
另请参见
- Ansible 剧本
剧本入门
- 角色
按角色组织剧本
- 用户邮件列表
有问题吗?访问 Google 群组!
- 实时聊天
如何加入 Ansible 聊天频道