You are currently viewing Ansible Playbook Components

Ansible Playbook Components

Ansible Playbook Components

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed Ansible Playbook.

In this blog post, we will discuss Ansible Playbook components.

Playbook Components:

  • Hosts: Using hosts, you can choose the target infrastructure where you want to execute the playbook. Hosts are defined in your inventory file and called in your playbook using hosts directive. You can use multiple hosts in the same playbook and each host can have a different task.
  • Users: Using remote_user (formerly called just user), you define user name that will be used to execute your task in the host. The scope of the remote_user can be entire playbook or an individual task depending upon the placement of remote_user block. remote_user also supports running tasks as another user using become directive.
  • Variables: Variables are a key component of the Ansible design. Variables allow for dynamic play content and reusable plays across different sets of inventories. Variables can be declared in inventory, playbook, included files and roles. There are other places (for example facts) where variables can come from, but these are a type of variable that is discovered, not set by the user.
  • Tasks: Using tasks, you define the action or play that will be performed in the target host. Each play contains a list of tasks. Tasks are executed in order, one at a time, against all machines matched by the host pattern. Every task should have a name, which is included in the output from running the playbook. Every task should have a name, which is included in the output from running the playbook. Tasks can be declared using the legacy action: module options format, but it is recommended that you use the more conventional module: options format. Ansible supports lots of modules that you can call in your play.
  • Handlers, Notify and Listen: Handlers are lists of tasks, not really any different from regular tasks, that are referenced by a globally unique name, and are notified by notifiers. If nothing notifies a handler, it will not run. Regardless of how many tasks notify a handler, it will run only once, after all of the tasks completed in a particular play. Handlers uses Listen method to listen to a notification sent by Notify.
  • Roles: Roles are ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure which allows easy sharing of roles with other users. Roles expect files to be in certain directory names (for example tasks) and must include at least one of these directories with a main.yml file, which contains the relevant content.

Playbook order of execution:

  • Variable loading
  • Fact gathering
  • The pre_tasks execution
  • Handlers notified from the pre_tasks execution
  • Roles execution
  • Tasks execution
  • Handlers notified from roles or tasks execution
  • The post_tasks execution
  • Handlers notified from post_tasks execution

Let us explain the order of execution and playbook components through a demo.

Ansible Playbook Component Demo:

Hope you have enjoyed this article. We will explore each component in details in its separate blog post. In the next blog post, we will discuss variables in Ansible.

To get more details on Ansible, please refer below Ansible documentation.