What is Ansible playbook?

Ansible Playbook

Ansible is a tool used for configuration management. In this context, configuration management entails keeping a record of company hardware and software, and making changes to them remotely.

Ansible Playbooks are the files where Ansible code is written. These files are written in the language, YAML, which is a funny acronym for, “YAML Ain’t no Markup Language.”

Playbooks contain one or more Plays. Plays map a group of computers to a few well-defined roles known as Tasks. Tasks can be defined as Ansible scripts.

YAML

YAML is a simple language that consists of key-value pairs. While not mandatory, --- marks the beginning of a YAML program, and ... marks the end.

---
john: #variable declared here
#data defined in following lines:
name: john doe
address: new york city
gender: male
...

The previous example defines the name, address, and gender of a fictionary person (John Doe) in the john variable.

YAML also allows the creation of lists. Lists can be created with the - symbol inside variables to define elements.

The following example defines a list of cities with their full name and state.

---
cities: #list declaration
- newYorkCity: #list element declared here
#element data defined here:
name: new york city
state: new york
- losAngeles: #list element declared here
#element data defined here:
name: los angeles
state: california
- philadelphia: #list element declared here
#element data defined here:
name: philadelphia
state: pennsylvania
...

Usage of Ansible playbook

For every play, a number of hosts and remote users are defined.

The hosts are a list of machines. The remote_user is the name of the user for which the play is being defined.

The tasks for a play are defined in the tasks key.

---
- hosts: all
remote_user: root
tasks:
- name: copy file
win_copy:
src: C:\source.txt
dest: C:\destination\
remote_src: yes
...

This example copies a file from a local Windows server to a remote Windows host. The actual functionality is defined in the win-copy line. The directory location for the source file is entered in src, and the directory location for the destination is entered in dest.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved