block
block module
Group related tasks together and apply common parameters. Blocks allow you to organize tasks logically and handle errors collectively.
Examples
- name: Install and configure web server
block:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx service
systemd:
name: nginx
state: started
enabled: true
- name: Copy configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
rescue:
- name: Handle installation failure
debug:
msg: "Failed to install web server"
always:
- name: Always run this
debug:
msg: "Block execution completed"
- name: Block with common when condition
block:
- name: Task 1
debug:
msg: "Running task 1"
- name: Task 2
debug:
msg: "Running task 2"
when: ansible_os_family == "Debian"
Note: Blocks support rescue and always sections for error handling, and can have when conditions that apply to all tasks in the block.
Module Capabilities
- Has Revert: false
- Provides Variables: None
Inputs
No input parameters.
Outputs
No outputs.