Bookmark this page

Building a Play with Multiple Tasks

Objectives

After completing this section, you should be able to build more complex plays that include multiple tasks.

Setting the Connection Method

Use connection: method to set the connection method in a playbook. Privilege escalation can be customized using become, become_user, and become_method.

---
- name: a simple playbook with one play and one task
  hosts: spine1
  connection: network_cli
  remote_user: admin
  become: yes
  become_method: enable
  vars:
    destination: 10.10.10.4

  tasks:
    - name: "test reachability to {{ destination }}"
      ios_ping:
        dest: "{{ destination }}"

Local Execution of Python Code

The connection methods for connecting to networking devices execute Ansible python code locally on the control node.

Long and Short Mapping Format Method

Mapping Shorthand or Long Form

For consistent formatting and improved readability, the recommended practice is to use the key: value syntax for mappings rather than the key=value shorthand.

The key: value form also facilitates fine-grained, line-oriented syntax error alerting.

The following is valid, but not recommended:

- name: print a message
  debug: msg="Welcome to the example.com network"

The preferred format is as follows:

- name: print a message
  debug:
    msg: "This is the example.com network"

Long and Short Sequence Format

Sequence Shorthand or Long Form

For similar reasons, using the longer - item block style rather than the shorter [ item,... ] flow style is recommended practice for representing sequences.

The following is valid, but not recommended:

vars:
  interfaces: [ GigabitEthernet1, GigabitEthernet2, GigabitEthernet3 ]

The preferred format is as follows:

vars:
  interfaces:
    - GigabitEthernet1
    - GigabitEthernet2
    - GigabitEthernet3

Notice that there are no commas after list elements in the preferred style.

Context Makes a Difference

In certain contexts, flow style may be preferred.

Sequences of Mappings

Flow style can be an effective way of promoting clarity, by conveying how elements in a collection are grouped together as a unit. This is frequently the case with sequences of mappings; with roles, for instance, or when loops or aggregation are used.

roles:
- { role: git_server, git_project: project }
- { role: git_server, git_project: ipa-client-register }
---
- hosts: routers
  gather_facts: no
  vars:
    interface_data:
      cs01:
        - { name: Loopback1, ipv4: 172.16.0.1/32 }
        - { name: GigabitEthernet2, ipv4: 172.16.2.2/30 }
        - { name: GigabitEthernet3, ipv4: 172.16.5.2/30 }
        - { name: GigabitEthernet4, ipv4: 172.16.10.1/30 }
  tasks:
    - name: set IP address on IOS device
      ios_l3_interface:
        aggregate: "{{ interface_data[inventory_hostname] }}"
      when: ansible_network_os == 'ios'

Multiline Strings

Look for opportunities to break long strings into multiline ones. For example:

vars:
  line: this is a very long line that seems to go on and on and on and on

The preferred format is shown below:

vars:
  line: >
    this is a very long line
    that seems to go on and on
    and on and on and on

Both of those are identical strings as far as YAML is concerned.

Assessing What Happened

The ansible-playbook command displays color-coded information about what is happening as plays are running and executing tasks on hosts.

Table 2.1. Color Key

ColorTextMeaning
greenokNormal, successful operation
yellowchangedHost was changed by task
rederrorError or failure

You can register the output of commands associated with a task, and conditionally perform later tasks based on the status of earlier ones (changed, ok, or error).

Understanding Play Output

The ansible-playbook command displays output in JSON form.

Play

Information about which hosts were targeted by the play

Task

Success or failure of task for host, plus any output from task

Gathering Facts

(unless suppressed): Success or failure of fact gathering for host

Play Recap

For each host, the number of tasks resulting in ok, changed, unreachable, and failed

Executing Ansible Playbooks

The ansible-playbook program runs the plays found in playbooks and much more. This is why it is good to name plays and tasks to clearly communicate their purpose.

List the hosts to which a playbook would be applied:

[user@host ~]$ ansible-playbook --list-hosts playbook-file

List the tasks contained in a playbook:

[user@host ~]$ ansible-playbook --list-tasks playbook-file

Verify the syntax of a playbook:

[user@host ~]$ ansible-playbook --syntax-check playbook-file

Return information about changes that would be made, but do not make them ("dry run"):

[user@host ~]$ ansible-playbook --check playbook-file

Power Plays

The first playbook example illustrated how to take an ad hoc command and convert it to a simple playbook. Plays do not begin to show their potential, however, until they do multiple tasks.

---
- name: play that sets interface descriptions
  hosts: cloud-services
  connection: network_cli
  tasks:

    - name: backup running config
      ios_config:
        backup: yes

    - name: label outside interface
      ios_interface:
        name: GigabitEthernet2
        description: outside

    - name: label inside interface
      ios_interface:
        name: GigabitEthernet4
        description: inside
Revision: do457-2.5-4693601