Playbook
Ansible

Executing Ansible Playbooks

Up to this point, you have installed Ansible, created the Ansible playbook directory structure and used roles as a part of that structure to define different devices. You have defined variables within YAML files of that directory structure for both universal and role specific use, and you have created the tasks for your common, underlay, and overlay roles that the playbook will execute using Ansible network modules and the variables you defined.

Some additional files as part of the Ansible directory structure need to created before you can kickoff the execution of your Ansible playbook to finish deploying your VXLAN EVPN fabric. You need to define the devices or hosts which you want the playbook to run against and you also need to define the playbook such that you run more than one play, i.e. use our roles and tasks define in the previous section.


Step 1 - Create Ansible Host File

Ansible refers to it's host file as an inventory file. The inventory file has a default location in /etc/ansible/hosts, but can also be specified directly within a playbook locally and used with the -i hosts option, where hosts happens to be the inventory file name. Within the inventory file, you can simply list all the devices/hosts or make use of group names using brackets which classifies devices you are controlling at what times and for what purpose.

Copy or type the below inventory host file.

    
        touch /home/cisco/Documents/nxapilab/ansible-nxos/staging.yml
        cat <<EOF >> /home/cisco/Documents/nxapilab/ansible-nxos/staging.yml
        # hosts file for Ansible playbook
        ---
        all:
          children:
            spines:
              hosts:
                10.15.6.11:
            leafs:
              hosts:
                10.15.6.12:
                10.15.6.13:
        EOF
        


Step 2 - Create the Main Ansible Playbook

As previously mentioned, Ansible calls it's configuration and orchestration framework "playbooks" and are a collections of "play(s)" or tasks for configuration management and deployment to a device or multiple devices. While playbooks can be represented as a single file, Ansible best practices recommend a particular directory structure for playbooks that you build using roles for better organization and reuse. You built all of this over the last three sections. You now need to build the main playbook file, which would look like the below:

The below playbook file you will use for this lab designates the following behaviors, for each role ‘x’:

  • If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
  • If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play
  • If roles/x/vars/main.yml exists, variables listed therein will be added to the play
  • If roles/x/defaults/main.yml exists, variables listed therein will be added to the play
  • If roles/x/meta/main.yml exists, any role dependencies listed therein will be added to the list of roles (1.3 and later)
  • Any copy, script, template or included tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely

Copy or type the main playbook YAML file.

    
        touch /home/cisco/Documents/nxapilab/ansible-nxos/vxlan.yml
        cat <<EOF >> /home/cisco/Documents/nxapilab/ansible-nxos/vxlan.yml
        ---
        # main playbook

        - hosts: spines, leafs
          gather_facts: false

          roles:
            - role: common
            - role: underlay

        - hosts: leafs
          gather_facts: false

          roles:
            - role: overlay

        - hosts: all
          gather_facts: false

          tasks:
            - name: Save Running-Config to Startup-Config
              cisco.nxos.nxos_config:
                save_when: always
        EOF
        


Step 3 - Execute the Ansible Playbook

To execute an Ansible playbook you simply just use ansible-playbook. You are going to use our own host file, so you must specify -i hosts, where -i is for inventory and hosts is the inventory file name. Lastly, you must specify the playbook file, site.yml.

    
        cd /home/cisco/Documents/nxapilab/ansible-nxos/
    
    
        ansible-playbook -i staging.yml vxlan.yml
    

Alternatively, you can add -vvv for verbose debugging output for each task that is executed.

Upon successful execution of this playbook, the spine and leaf roles, and the tasks and variables defined in each, will configure VXLAN EVPN VLANs, VNIs, SVIs, Tenant VRFs, etc.


    PLAY RECAP ***********************************************************************************************************************************************************************************************
    10.15.6.11                 : ok=14   changed=12   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    10.15.6.12                 : ok=27   changed=25   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
    10.15.6.13                 : ok=27   changed=25   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

Continue to the next section for writing code to test your network using Cisco pyATS!