Bookmark this page

Lab: Parameterizing Automation

In this lab, you will set host and domain names and nameservers on managed devices in the Lab Network. In the course of doing so, you will review the process of using variables, loops and conditionals, roles, and Jinja2 templates to affect the outcome of automated processes.

Outcomes

You should be able to:

  • Add variables that provide data supporting our objectives.

  • Create a Jinja2 template that transforms the data into commands.

  • Compose a playbook that sources commands from the Jinja2 template you created.

  • Perform the play, restricting its scope to a single target.

  • Commit your work to the Git repository.

Open a terminal window on the workstation VM. Change to a directory that contains your latest updates to your ansible-generic project. This directory, together with an appropriately protected Vault password file, supports the ability to perform tasks on remote devices in the Lab Network.

Instructions

Perform the following steps:

  • Add group variables for domain name and name servers. The host name is automatically available by virtue of the inventory_hostname variable.

  • Create two Jinja2 templates that convert data into commands: one for VyOS devices and one for IOS devices. Name them vyos-hn-dn-ns.j2 and ios-hn-dn-ns.j2, respectively.

  • Create two playbooks named j2test-vyos-hn-dn-ns.yml and j2test-ios-hn-dn-ns.yml. The connection method should be set to local in these playbooks. Each of the playbooks should contain a play consisting of a single task, which uses the template module to simply write to a file or series of files (one per inventory_hostname) the document produced by the corresponding template. Use these playbooks to verify that the templates do indeed generate the desired configuration statements.

  • Create a playbook named set-hn-dn-ns.yml that sets host name, domain name, and nameservers for networking devices by sourcing configuration statements directly from an appropriate Jinja2 template. The template should be selected according to the value of the ansible_network_os variable.

  • Perform the play in the set-hn-dn-ns.yml playbook, restricting it to a single target.

  • Commit your work to the Git repository. If prompted for credentials, user and password are student and student. The commands are shown here.

    [student@workstation ansible-generic-project]$ git add -A :/
    [student@workstation ansible-generic-project]$ git commit -m "ch7 lab3"
    [student@workstation ansible-generic-project]$ git push
  1. Add group variables for domain name and name servers. The host name is automatically available by virtue of the inventory_hostname variable.

    Add a domain_name variable and a name_servers variable to the network group.

    [student@workstation ansible-generic-project]$ cat group_vars/network/vars.yml
    ansible_connection: network_cli
    domain_name: lab.example.com
    name_servers:
    - 8.8.8.8
    - 8.8.4.4
  2. Create two Jinja2 templates that convert data into commands: one for VyOS devices and one for IOS devices. Name them vyos-hn-dn-ns.j2 and ios-hn-dn-ns.j2, respectively.

    1. If it does not already exist, create a j2 directory to hold Jinja2 template files.

      [student@workstation ansible-generic-project]$ mkdir j2
    2. Create a Jinja2 template named j2/vyos-hn-dn-ns.j2 that generates host name, domain name and nameserver configuration statements for VyOS systems.

      [student@workstation ansible-generic-project]$ cat j2/vyos-hn-dn-ns.j2
      set system host-name {{ inventory_hostname }}
      set system domain-name {{ domain_name }}
      {% for ns in name_servers %}
      set system name-server {{ ns }}
      {% endfor %}
    3. Create a Jinja2 template named j2/ios-hn-dn-ns.j2 that generates domain name and nameserver configuration statements for IOS systems.

      [student@workstation ansible-generic-project]$ cat j2/ios-hn-dn-ns.j2
      hostname {{ inventory_hostname }}
      ip domain name {{ domain_name }}
      {% for ns in name_servers %}
      ip name-server {{ ns }}
      {% endfor %}
  3. Create two playbooks named j2test-vyos-hn-dn-ns.yml and j2test-ios-hn-dn-ns.yml. The connection method should be set to local in these playbooks. Each of the playbooks should contain a play consisting of a single task, which uses the template module to simply write to a file or series of files (one per inventory_hostname) the document produced by the corresponding template. Use these playbooks to verify that the templates do indeed generate the desired commands.

    1. Create a directory to hold files your playbooks create.

      [student@workstation ansible-generic-project]$ mkdir out
    2. Create the j2test-vyos-hn-dn-ns.yml playbook that generates VyOS configuration statements for a representative VyOS device and writes them to a file.

      [student@workstation ansible-generic-project]$ cat j2test-vyos-hn-dn-ns.yml
      ---
      - name: generate VyOS hostname, domain name, and nameserver statements
        hosts: spine01
        connection: local
        vars:
          tmpl: j2/vyos-hn-dn-ns.j2
          outfile: out/vyos-hn-dn-ns-{{ inventory_hostname }}.cmd
      
        tasks:
      
        - name: read from template, write to output file
          template:
            src:  "{{ tmpl }}"
            dest: "{{ outfile }}"
    3. Perform the play in the j2test-vyos-hn-dn-ns.yml playbook.

      [student@workstation ansible-generic-project]$ ansible-playbook \
      > j2test-vyos-hn-dn-ns.yml
    4. Confirm that it generates VyOS configuration statements as desired.

      [student@workstation ansible-generic-project]$ cat out/vyos-hn-dn-ns-spine01.cmd
      set system host-name spine01
      set system domain-name lab.example.com
      set system name-server 8.8.8.8
      set system name-server 8.8.4.4
    5. Create the j2test-ios-hn-dn-ns.yml playbook that generates IOS configuration statements for a representative IOS device and writes them to a file.

      [student@workstation ansible-generic-project]$ cat j2test-ios-hn-dn-ns.yml
      ---
      - name: generate ios hostname, domain name, and nameserver statements
        hosts: cs01
        connection: local
        vars:
          tmpl: j2/ios-hn-dn-ns.j2
          outfile: out/ios-hn-dn-ns-{{ inventory_hostname }}.cmd
      
        tasks:
      
        - name: read from template, write to output file
          template:
            src:  "{{ tmpl }}"
            dest: "{{ outfile }}"
    6. Perform the play in the j2test-ios-hn-dn-ns.yml playbook.

      [student@workstation ansible-generic-project]$ ansible-playbook \
      > j2test-ios-hn-dn-ns.yml
    7. Confirm that it generates IOS configuration statements as desired.

      [student@workstation ansible-generic-project]$ cat out/ios-hn-dn-ns-cs01.cmd
      set system host-name cs01
      set system domain-name lab.example.com
      set system name-server 8.8.8.8
      set system name-server 8.8.4.4
  4. Create a playbook named set-hn-dn-ns.yml that sets host name, domain name, and nameservers for networking devices by sourcing configuration statements directly from an appropriate Jinja2 template. The template should be selected according to the value of the ansible_network_os variable.

    Create a multivendor playbook named set-hn-dn-ns.yml that uses the when conditional to perform or skip tasks based on the value of the ansible_network_os variable. The connection method for these should be network_cli, NOT local. The playbook consists of a play that sets host name, domain name, and nameservers for both VyOS and IOS devices:

    [student@workstation ansible-generic-project]$ cat set-hn-dn-ns.yml
    ---
    - name: configure hostname, domain name, and nameserver
      hosts: network
      vars:
        tmpl: j2/{{ ansible_network_os }}-hn-dn-ns.j2
    
      tasks:
    
      - name: read from template, configure IOS device
        ios_config:
          src:  "{{ tmpl }}"
        when: ansible_network_os == 'ios'
    
      - name: read from template, configure VyOS device
        vyos_config:
          src:  "{{ tmpl }}"
        when: ansible_network_os == 'vyos'
  5. Perform the play in the set-hn-dn-ns.yml playbook, limiting it to a single target.

    [student@workstation ansible-generic-project]$ ansible-playbook -l spine01 \
    > set-hn-dn-ns.yml
  6. Compose a playbook named ios-vyos-assert-domain.yml that asserts that the domain name is currently set to"lab.example.com".

    [student@workstation ansible-generic-project]$ cat ios-vyos-assert-domain.yml
    ---
    - name: multi-vendor play that asserts value of domain name
      hosts: network
      vars:
        correct_domain: lab.example.com
        ios_command: sh run | include domain
        vyos_command: sh conf comm | grep domain-name
    
      tasks:
    
      - name: "{{ ios_command }} on IOS device"
        ios_command:
          commands:
          - "{{ ios_command }}"
        register: ios_result
        when: ansible_network_os == 'ios'
    
      - name: "assert IOS device is in {{ correct_domain }}"
        assert:
          that: "'{{ correct_domain }}' in ios_result.stdout[0]"
        when: ansible_network_os == 'ios'
    
      - name: "{{ vyos_command }} on VyOS device"
        vyos_command:
          commands:
          - "{{ vyos_command }}"
        register: vyos_result
        when: ansible_network_os == 'vyos'
    
      - name: "assert VyOS device is in {{ correct_domain }}"
        assert:
          that: "'{{ correct_domain }}' in vyos_result.stdout[0]"
        when: ansible_network_os == 'vyos'
  7. Perform the play you created that asserts the value of the domain name.

    [student@workstation ansible-generic-project]$ ansible-playbook \
    > ios-vyos-assert-domain.yml
  8. Commit your work to the Git repository. If prompted for credentials, user and password are student and student.

    [student@workstation ansible-generic-project]$ git add -A :/ && git commit -m \
    > "ch7 lab3" && git push
Revision: do457-2.5-4693601