Ansible Tasks

Learn how to write Ansible tasks.

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:

Press + to interact
- 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:

Press + to interact
- name: Ensure Nginx configtest passes
command: nginx -t
register: nginx_config_test
ignore_errors: yes
become: yes
  • Line 2: The task uses the command ...