Bookmark this page

Preparing Ansible Playbooks

Objectives

After completing this section, you should be able to:

  • Create a simple playbook.

  • Check playbook syntax with ansible-playbook --syntax-check.

Tasks

A task is the application of a module to perform a specific unit of work. An example of a task in a play is shown below.

tasks:
  - name: Backup configuration
    ios_config:
      backup: yes

Plays and Playbooks

A play is a sequence of tasks to be applied, in order, to one or more hosts, whereas a playbook is a YAML file containing one or more plays.

Visualizing Plays and Playbooks

An example of a play in a playbook is shown below.

---
- name: backup router configurations
  hosts: routers
  connection: network_cli
  gather_facts: no

  tasks:
    - name: gather ios_facts
      ios_facts:
      register: version

    - debug:
      msg: "{{version}}"

    - name: Backup configuration
      ios_config:
        backup: yes

Interpreting YAML

YAML is a human-friendly language that concisely represents objects as data. Additional characteristics of YAML include:

  • Files start with three dashes (---) that mark the start of the document.

  • Comments begin with the pound sign (#).

  • Indentation is significant, and spaces must be used rather than tabs.

  • Elements at the same level (items in the same list, for instance) must have the same indentation.

  • Children of an item are indented more than the parent.

YAML Object Types Using Block Style

Unquoted scalar

Network

Sequence of scalars

- 10.0.0.1
- 192.168.0.1
- 172.16.0.1

Mapping scalars to scalars

IPv4: 10.10.17.42/24
IPv6: "fd42:e5a1:ef5d:6030:0:0:0:2/64"

Mapping scalars to sequences

rtr01:
  - GigabitEthernet1
  - GigabitEthernet2

rtr02:
  - eth0
  - eth1

Sequence of mappings

-
  name: GigabitEthernet2
  ipv4: 172.16.2.2/30
  ipv6: "fd42:e5a1:ef5d:6030:0:0:0:2/64"
-
  name: eth1
  ipv4: 10.10.10.1/30
  ipv6: "fdbc:bda:8486:7118:0:0:0:1/64"

Mapping of mappings

vyos:
  ansible_network_os: vyos
  ansible_user: vyos

ios:
  ansible_network_os: ios
  ansible_user: admin

YAML Object Types Using Flow Style

Sequence of scalars

[ GigabitEthernet1, GigabitEthernet2, GigabitEthernet3 ]

Mapping scalars to scalars

{ name: eth1, ipv4: 10.10.5.1/30, ipv6: "fdb5:4b4e:4574:c6bb:0:0:0:1/64" }

Sequence of sequences

- [name, ipv4, ipv6]
- [GigabitEthernet1, 172.16.2.2/30, "fdb5:4b4e:4574:c6bb:0:0:0:1/64"]
- [GigabitEthernet2, 10.10.5.2/30, "fdb5:4b4e:4574:c6bb:0:0:0:2/64"]

Sequence of mappings

- { name: GigabitEthernet2, ipv4: 172.16.2.2/30 }
- { name: GigabitEthernet4, ipv4: 172.16.10.1/30 }

Mapping of mappings

GigabitEthernet1: {ipv4: 10.0.0.1/30, ipv6: "fdb5:4b4e:4574:c6bb::1/64"}
GigabitEthernet2: {
    ipv4: 192.168.5.1/30,
    ipv6: "fdea:230f:c3cf:c287:0:0:0:1/64"
}

Encoding Plays in YAML

How are Ansible plays encoded in YAML?

  • A playbook is a list of one or more plays.

  • Each play is a hash/dictionary; that is to say, a YAML sequence of key:value mappings.

  • A play must include host and tasks mappings, and may include a name mapping, as well as other mappings depending on which plug-ins are being used.

  • Plays can import or include files, other playbooks, task lists, and so on.

Mapping the Terrain

Which key:value pairs are used to define plays in playbooks?

  • Plays can be named or anonymous. It is useful to name your plays. For example, name:playname. Ideally, a name communicates clearly the purpose of a play.

  • Plays take a list of tasks and apply them to a list of hosts or host groups.

---
- name: a play that backs up configs
  hosts:
    - routers

  tasks:
    - name: backup the running config
      ios_config:
        backup: yes

Choosing a Module: ping or ios_ping

ping

  • A trivial test module that always returns pong on successful contact.

  • This is not an ICMP ping, and requires Python on the remote node.

  • It usually does not make sense in playbooks, but is useful from /usr/bin/ansible to verify the ability to log in and that a usable Python is configured.

ios_ping

  • Tests reachability using ping from network device to a remote destination using available routes. This module is specific to devices running the IOS network operating system.

Running ios_ping Ad Hoc Command

Tests reachability by pinging from an IOS network device to a remote destination.

[user@host ~]$ ansible -m ios_ping host-identifier -a "dest=ip-address"

Using ios_ping in a Playbook

Tests reachability, in a playbook, by pinging from a network device to a remote destination.

---
- name: a reachability test
  hosts: rtr1

  tasks:
    - name: "test reachability to rtr1"
      ios_ping:
        dest: rtr1
Revision: do457-2.5-4693601