In this exercise, you will configure an IOS device with static host objects using commands sourced directly from a Jinja2 template.
Outcomes
You should be able to:
Create a data variable that maps names to IP addresses.
Create a Jinja2 template that loops over the data, generating an IOS ip host command for each row of data.
Write a play that uses the template module and the local connection method to write the set of commands generated by your Jinja2 template to a file.
Write a play that uses the ios_config module (connection method network_cli) to configure IOS devices directly from the set of commands generated by your Jinja2 template.
Open a terminal window on the workstation VM and change to your ~/proj directory.
Procedure 3.5. Instructions
Create a data variable that maps names to IP addresses.
Download the host-objects.yml file into the vars subdirectory:
[student@workstation proj]$mkdir -p vars[student@workstation proj]$cd vars[student@workstation vars]$wget \>http://materials.example.com/full/vars/host-objects.yml[student@workstation vars]$cd ..[student@workstation proj]$cat vars/host-objects.ymlhost_object_data: - { name: spine01, ipv4: 10.0.0.1/32 } - { name: spine02, ipv4: 10.0.0.11/32 } - { name: leaf01, ipv4: 192.168.0.1/32 } - { name: leaf02, ipv4: 192.168.0.2/32 } - { name: cs01, ipv4: 172.16.0.1/32 } - { name: server01, ipv4: 10.10.10.2 } - { name: server02, ipv4: 192.168.10.2 } - { name: server03, ipv4: 172.16.10.2 }
Create a Jinja2 template that loops over the data, generating an IOS ip host command for each row of data.
Create a file named j2/ios-host-objects.j2 with the following content:
{% for obj in host_object_data %}
ip host {{ obj.name }} {{ obj.ipv4 | ipaddr('address') }}
{% endfor %}
Write a play that uses the template module and connection method local to write the set of commands generated by your Jinja2 template to a file.
Create a playbook named j2test-ios-host-objects.yml that defines a task that dumps text from your Jinja2 template out to a file named out/ios-host-objects.cmd.
The playbook should look similar to the following:
---
- name: build a file containing IOS static host object statements
hosts: localhost
connection: local
vars:
srcfile: j2/ios-host-objects.j2
destfile: out/ios-host-objects.cmd
vars_files:
- vars/host-objects.yml
tasks:
- name: dump output from template to a file
template:
src: "{{ srcfile }}"
dest: "{{ destfile }}"
Before you run the play, create the out directory that will hold the file created by the template module:
[student@workstation proj]$mkdir out[student@workstation proj]$echo 'directory for generated output' > out/README
Run the play found in your new playbook. Type any text when prompted for a password, because it is not used.
[student@workstation proj]$ansible-playbook j2test-ios-host-objects.ymlSSH password:anything
View the output file containing the generated list of IOS ip host commands:
[student@workstation proj]$cat out/ios-host-objects.cmdip host spine01 10.0.0.1 ip host spine02 10.0.0.11 ip host leaf01 192.168.0.1 ip host leaf02 192.168.0.2 ip host cs01 172.16.0.1 ip host server01 10.10.10.2 ip host server02 192.168.10.2 ip host server03 172.16.10.2
Write a play that uses the ios_config module (connection method network_cli) to configure IOS devices directly from the set of commands generated by your Jinja2 template.
Create a playbook named ios-host-objects.yml.
It uses the src attribute of the ios_config module to source the config lines directly from the Jinja2 template: The ios-host-objects.yml playbook must include the following:
---
- name: build a file containing IOS static host object statements
hosts: ios
vars:
srcfile: j2/ios-host-objects.j2
vars_files:
- vars/host-objects.yml
tasks:
- name: configure directly from template
ios_config:
src: "{{ srcfile }}"
when: ansible_network_os == 'ios'
Run the play, and this time specify student as the SSH password.
[student@workstation proj]$ansible-playbook ios-host-objects.ymlSSH password:student
Execute an ad hoc command to confirm that the change was successful:
[student@workstation proj]$ansible -m ios_command \>-a "commands='sh run | include ip host'" cs01SSH password:studentcs01 | SUCCESS => { "changed": false, "stdout": [ "ip host cs01 172.16.0.1\nip host leaf01 192.168.0.1\nip host leaf02 192.168.0.2\nip host server01 10.10.10.2\nip host server02 192.168.10.2\nip host server03 172.16.10.2 ip host spine01 10.0.0.1\nip host spine02 10.0.0.11\n" ], "stdout_lines": [ [ "ip host cs01 172.16.0.1", "ip host leaf01 192.168.0.1", "ip host leaf02 192.168.0.2", "ip host server01 10.10.10.2", "ip host server02 192.168.10.2", "ip host server03 172.16.10.2" "ip host spine01 10.0.0.1", "ip host spine02 10.0.0.11", ] ] }
This concludes the guided exercise.