In this exercise, you will build a play with multiple tasks.
Outcomes
You should be able to:
Construct a playbook that uses multiple tasks.
Verify the YAML syntax of the playbook.
Run the play.
It is assumed you know how to set the value of variables to support connection and authentication, as described in the the section called “Lab: Deploying Ansible” exercise.
Open a terminal window as the student user on the workstation VM.
Change to the ~/proj/ directory created in Lab 1.
Use your preferred text editor to create a file called ios-checksys1.yml with the following contents:
---
- name: back up config and look at device health indicators on ios devices
hosts: ios
tasks:
- name: backup the device configuration
ios_config:
backup: yes
- name: look at device health indicators
ios_command:
commands:
# this provides hostname and uptime
- sh ver | include uptime
- sh ip domain
- sh clock
- sh ip name-server
- sh proc mem | include Total
register: results
- name: show results
debug:
msg: "{{ item }}"
loop: "{{ results.stdout_lines }}"YAML is extremely strict about formatting, so pay special attention to indentation. Use two spaces (not tabs) when indenting lines. If you configured Vim as specified in the preceding exercise, then Tab indents by two spaces.
It is considered good practice to back up the running configuration for network devices often. This playbook displays several key system indicators (uptime, domain name, clock time, and so forth), and also makes a backup copy of the current (running) configuration.
The register module associates the output of a command with a variable.
In this example the variable name is results.
The debug module displays the stdout property of the results variable.
This play also introduces a loop concept, which is explored in more depth later.
Check the syntax of the playbook you created.
[student@workstation proj]$ansible-playbook --syntax-check ios-checksys1.ymlplaybook: ios-checksys1.yml
Use the ansible-playbook command to run the play in your playbook:
[student@workstation proj]$ansible-playbook ios-checksys1.ymlSSH password:studentPLAY [back up config and look at device health indicators on ios devices] ****** TASK [backup the device configuration] ***************************************** ok: [cs01] TASK [look at device health indicators] **************************************** ok: [cs01] TASK [show results] ************************************************************ ok: [cs01] => (item=None) => { "msg": [ "cs01 uptime is 2 hours, 38 minutes" ] } ok: [cs01] => (item=None) => { "msg": [ "lab.example.com" ] } ok: [cs01] => (item=None) => { "msg": [ "*19:06:11.781 UTC Sun Jul 26 2020" ] } ok: [cs01] => (item=None) => { "msg": [ "255.255.255.255" ] } ok: [cs01] => (item=None) => { "msg": [ "Processor Pool Total: 2092339280 Used: 333676544 Free: 1758662736", " lsmpi_io Pool Total: 3149400 Used: 3148568 Free: 832", " 336777680 Total" ] } PLAY RECAP ********************************************************************* cs01 : ok=3 changed=0 unreachable=0 failed=0
This concludes the guided exercise.