Two Ansible playbook examples illustrate config management (original) (raw)

Configuration management enables automation for many operations tasks. Ansible uses a simple structure and a dynamic language to enable admins to easily complete jobs, once they get the hang of a playbook.

As configuration management tools go, Ansible is one of the easiest to learn. Plays live in playbooks, which are simple to write once you master YAML formatting and Ansible structure.

Editor's note: If you're new to Ansible, first, read about the structure of Ansible configuration management. This article delves into two Ansible playbook examples that show common elements that IT teams use for configuration automation and assumes some knowledge of how Ansible works.

Ansible plays contain the commands that a user wishes to execute on given hosts. A play is one task, and as its name implies, a playbook gathers together multiple plays to achieve a configuration. These instructions are executed one by one. An Ansible playbook presents IT organizations with a human- and machine-readable way to describe infrastructure configurations, build containers and do other tasks. Playbooks can become complex, with multiple advanced features included to make the configuration as flexible as possible. The core elements are Ansible modules and the YAML language.

Fluency in YAML

All Ansible playbooks are written in YAML. While the acronym stands for YAML Ain't Markup Language, users can consider it as a markup language to gain a basic understanding of how it works. The Ansible user defines items in a YAML file, along with the properties that apply to each of these items.

Listing 1: This Ansible playbook example shows the role that indentation has in organizing a YAML file.

YAML file organization

Ansible playbooks, as in Listing 1, start with ---, although this practice is not mandatory. The --- inclusion ensures that the system easily recognizes the code as being in the YAML format that it uses.

Indentation is essential to clearly distinguish between the item and its properties.

Indentation is essential to clearly distinguish between the item and its properties. Indentation is achieved with spaces. The example playbook in Listing 1 includes a name, the hosts to which it applies and the tasks to execute. The name, hosts and tasks are indented at the same level. The following indentation level is for the task information, which contains two different items as well that we will not dig into here.

Let a virtualization expert take you through the construction of a playbook and how to set it in motion, all in under 10 minutes.

Time to feel included

Focus on the include line, which inputs the contents of another YAML file into the Ansible playbook. Ansible users commonly apply this approach: Rather than include all of the code in the same playbook, they work with different, related files. Users can distinguish between the components in a configuration more easily with this approach. For example, the main playbook uses the include statement, followed by the name of the file that is to be included, tasks.yml. Listing 2 shows a typical include file.

Listing 2: This YAML-written Ansible playbook example is an include file.

Ansible playbook include file layout

Bring in modules and variables

The contents of the include file in Listing 2 use two Ansible modules: yum and service. Ansible modules are written in Python and provide the flexibility to run any code from an Ansible playbook. The Ansible community creates many default modules, and they're relatively easy to develop to fill a particular need.

Listing 2 showcases another element seen regularly in Ansible playbooks: the variable. The variables in the include file refer to values that are set in the main Ansible file. Variables help users clearly distinguish between fixed and dynamic parts of a playbook.

Ansible's YAML file format, modules, include command and variables are a good start to automate tasks in the IT environment. Users can master the elements portrayed in these Ansible playbook examples and develop more sophisticated infrastructure-as-code deployments as they explore the configuration management tool.

Dig Deeper on Systems automation and orchestration