Introduction to Ansible playbooks – Infrastructure as Code (IaC) with Terraform

Imagine you’re a conductor leading an orchestra. In this scenario, Ansible playbooks are akin to your musical score, guiding every musician to create a harmonious symphony of automation in the tech world.

In the realm of tech and automation, Ansible playbooks provide the following:

  • Musical score for automation: Just as a conductor uses a musical score with notations to guide each instrument, an Ansible playbook contains a set of instructions and actions for orchestrating specific IT tasks and configurations, spanning from software deployments to system configurations.
  • Harmonious guidance: Ansible playbooks take a similar approach. You declare the desired IT state, and Ansible plays the role of the conductor, ensuring that all the necessary steps are followed, much like specifying, “I want a flawless musical performance,” and Ansible orchestrates the entire process.
  • Tasks and reusability: Ansible playbooks are organized into tasks and roles, as with musical sheets and instruments. These tasks can be reused across various playbooks, promoting consistency and saving time.
    • Instrument selection and direction: Just as a conductor selects which instruments play at which times, playbooks specify which servers or machines (the inventory) should execute tasks. You can direct specific server groups or individual machines.
  • Harmonious execution: Ansible can skilfully coordinate tasks on multiple machines simultaneously, much as a conductor harmonizes the efforts of different musicians to create a beautiful composition.
    • Fine-tuned performance: If unexpected challenges arise during the performance, a conductor adjusts and guides the musicians to ensure a flawless outcome. Similarly, Ansible playbooks incorporate error-handling strategies to handle unexpected issues during automation.

Ansible playbooks are a collection of tasks that produce the desired configuration within the managed nodes. They have the following features:

  • They help in managing configuration within multiple remote servers using declarative steps
  • They use a sequential list of idempotent steps, and steps that match the expected configuration are not applied again
  • Tasks within the playbook can be synchronous and asynchronous
  • They enable GitOps by allowing the steps to be stored using a simple YAML file to keep in source control, providing CaC

Ansible playbooks consist of multiple plays, and each play is mapped to a group of hosts using a role and consists of a series of tasks required to achieve them—something like the following diagram:

Figure 9.2 – Playbooks

The following ping.yaml file is an example of a simple playbook that pings all servers:

  • hosts: all tasks:
    • name: Ping all servers

action: ping

The YAML file contains a list of plays, as the list directive shows. Each play consists of a hosts attribute that defines the role to which we want to apply the play. The tasks section consists of a list of tasks, each with name and action attributes. In the preceding example, we have a single play with a single task that pings all servers.

Leave a Reply

Your email address will not be published. Required fields are marked *