After completing this section, you should be able to use Ansible to apply configuration directives that have been generated from a Jinja2 template to a network device.
As a general rule, an *os_config module is the best choice for applying changes to devices.
But there are exceptions; sometimes a special purpose module exists.
The ios_banner module gracefully handles multiline banners.
---
- name: play for setting banner motd on ios devices
hosts: leaf1
gather_facts: False
tasks:
- name: set banner motd
ios_banner:
banner: motd
test: |
Unuauthorized access to this device is prohibited
state: presentConfiguration statements can be embedded directly within playbooks:
---
- hosts: ios
tasks:
- ios_config:
lines: hostname "{{ inventory_hostname }}"Configuration statements can also be sourced from a local file:
---
- name: A playbook that uses a static file to configure Cisco network devices
hosts: ios
tasks:
- set_fact:
config_filepath: "{{ playbook_dir }}/configs"
- name: configure using static config file
ios_config:
src "{{ config_filepath }}/{{ inventory_hostname }}.cfgJinja2 templates can be used to generate the files sourced to configure devices.
---
- name: A play that uses a Jinja2 template to generate a set of config files
hosts: ios
connection: local
tasks:
- template:
src: ios-leaf-common.j2
dest: "{{ config_filepath }}/{{ inventory_hostname }}.cfg"
- name: A play that applies configs generated above to network devices
hosts: ios
tasks:
- name: configure using static config file
ios_config:
src: "{{ config_filepath}}/{{ inventory_hostname }}.cfg
- name: save running to startup when modified
ios_config:
save_when: modified
Ansible *os_config modules are designed to support Jinja2 templates directly, so you can generate configurations and apply them in with a single task.
---
- name: A play that generates configs and applies them in one step
hosts: ios
tasks:
- name: configure using static config file
ios_config:
src: ios-leaf.j2
- name: save running to startup when modified
ios_config:
save_when: modified