Bookmark this page

Guided Exercise: Composing Playbooks with Multiple Plays

In this exercise, you will compose a playbook that contains multiple plays. Each play will target a different host. Different targets will have different login passwords. This course has not covered how to use Ansible Vault to encrypt files or variables containing sensitive data yet, so the --extra-vars option is used to pass two different passwords to two different plays in the same playbook.

Outcomes

You should be able to:

  • Compose a playbook that has multiple plays.

  • Verify the YAML syntax of the playbook.

  • Run the plays.

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 on the workstation VM.

  1. Change to the ~/proj/ directory created in Lab 1.

  2. Create a file called multi-vendor-backup.yml with the following contents:

    ---
    - name: back up config from a VyOS device
      hosts: spine01
      vars:
        ansible_password: "{{ vyos_pass }}"
    
      tasks:
    
      - name: back up config
        vyos_config:
          backup: yes
    
    - name: back up config from an IOS device
      hosts: cs01
      vars:
        ansible_password: "{{ ios_pass }}"
    
      tasks:
    
      - name: back up config
        ios_config:
          backup: yes

    Different Ansible modules are used to back up network devices that run different operating systems. The ios_backup module backs up the running configuration for devices that run IOS and the vyos_backup module backs up the configuration for devices that run VyOS. How do you make sure that one way of doing a task applies to one set of hosts and a different way is used when dealing with different hosts? One way is to use different plays.

    This playbook illustrates the fact that a single playbook can hold multiple plays, each of which maps a particular set of tasks to a set of hosts.

  3. Check the syntax of the playbook you created:

    [student@workstation proj]$ ansible-playbook --syntax-check multi-vendor-backup.yml
    
    Playbook: multi-vendor-backup.yml
  4. Use the ansible-playbook command to perform the plays in the multi-vendor-backup.yml playbook. Circumvent the restriction of using one group at a time by using the --extra-vars option to pass in the SSH passwords needed. You can provide any password at the SSH prompt; it is not used.

    [student@workstation proj]$ ansible-playbook \
    > -e 'vyos_pass=vyos ios_pass=student' multi-vendor-backup.yml
    SSH password: anything
    
    PLAY [back up config from a VyOS device] ***************************************
    
    TASK [back up config] **********************************************************
    ok: [spine01]
    
    PLAY [back up config from an IOS device] ***************************************
    
    TASK [back up config] **********************************************************
    ok: [cs01]
    
    PLAY RECAP *********************************************************************
    cs01                      : ok=1   changed=0   unreachable=0   failed=0
    spine01                   : ok=1   changed=0   unreachable=0   failed=0

This concludes the guided exercise.

Revision: do457-2.5-4693601