Search⌘ K
AI Features

Ansible Tasks

Explore the core concepts of Ansible tasks and learn to define and sequence them effectively within playbooks. Understand the use of modules, loops, and conditional statements to automate complex actions. Gain practical skills in managing task execution order, error handling, and creating automated workflows to configure directories, files, and commands on remote hosts.

What are tasks?

A task represents a single action within a playbook that we can perform on a managed node. Tasks make up plays and leverage modules to perform specific actions like installing software, copying files, managing users, etc.

In order to define a task, we have to specify a name, the module we’d like to use, and other relevant parameters. Here’s the basic syntax for defining a task:

YAML
- name: Description of the task
module_name:
module_parameter1: value1
module_parameter2: value2
# other task options like become, when etc.

In the syntax above, a task contains the following parameters:

  • name: Describes the task in a human-readable manner, e.g., “Installing NGINX.”
  • module_name: Used to specify the Ansible module to be used in the task in order to achieve the specific action
  • module_parameter: The arguments that we need to pass to the module that we choose for our task.

A working example of a task is shown below:

YAML
- name: Ensure Nginx configtest passes
command: nginx -t
register: nginx_config_test
ignore_errors: yes
become: yes
  • Line 2: The task uses the command module within an Ansible playbook to execute the nginx -t command on a managed node. This command is typically used to perform a configuration test for the NGINX web server and check ...