Ansible User Guide
Steps to use an Ansible Playbook
In the below example, we will show steps to use an Ansible Playbook using Jinja2 template and sample parameter file. This example show how to configure LDP protocol:
ocnos_ldp.j2
osboxes@osboxes:~/playbooks$ cat templates/ocnos_ldp.j2
{%if ldp is defined%}
router ldp
{% for peer in ldp.peers -%}
targeted-peer ipv4 {{ peer.address }}
exit
{% endfor %}
{% if ldp.advertise_label_prefix is defined %}
advertise-labels for only_lo to any
{%endif%}
{% for interface in ldp.interfaces -%}
interface {{ interface.name }}
enable-ldp {{ interface.protocol }}
label-switching
exit
{% endfor %}
{%endif%}
We will provide the LDP configuration details in the appropriate host_vars file.
sw2.yml
osboxes@osboxes:~/playbooks$ cat host_vars/sw2.yml
ldp:
peers:
- address: 1.1.1.1
- address: 3.3.3.3
advertise_label_prefix: only_lo
ldp_interfaces:
- { ldp_interface: eth1, ldp_protocol: ipv4 }
- { ldp_interface: eth2, ldp_protocol: ipv4 }
Here is the overall directory structure of the Ansible Playbook and associated files.
osboxes@osboxes:~/playbooks$ tree
|___ansible.cfg
|___backup
|___group_vars
|___ocnos.yml
|___hosts-net
|___host_vars
|___sw2.yml
|___ldp-playbook.yml
|___showldp-playbook.yml
|___templates
|___ocnos_ldp.j2
The following is the content of ansible.cfg file which points to hosts-net inventory file.
ansible.cfg
osboxes@osboxes:~/playbooks$ cat ansible.cfg
[defaults]
inventory = hosts-net
host_key_checking = False
retry_files_enabled = False
interpreter_python = auto
osboxes@osboxes:~/playbooks
Following is the content of the hosts-net inventory file. Currently this has details of only one device.
hosts-net
osboxes@osboxes:~/playbooks$ cat hosts-net
[ocnos]
sw2 ansible_host=10.12.9.105
osboxes@osboxes:~/playbooks$
Following is the content of ocnos.yml in group_vars folder.
ocnos.yml
osboxes@osboxes:~/playbooks$ cat group_vars/ocnos.yml
ansible_connection: network_cli
ansible_network_os: ocnos
ansible_become: yes
ansible_become_method: enable
ansible_ssh_user: ocnos
ansible_ssh_pass: ocnos
osboxes@osboxes:
The below playbook pushes the ldp configuration created using the template file 'ocnos_ldp.j2' for all the ocnos hosts using the appropriate host_vars file 'sw2.yml':
ldp-playbook.yml
(ansible) osboxes@osboxes:~/playbooks$ cat ldp-playbook.yml
---
- hosts: ocnos
gather_facts: no
tasks:
- name: configure LDP config on OcNOS
cli_config:
config: "{{ lookup('template', 'templates/{{ ansible_network_os }}_ldp.j2') }}"
Configuration on the OcNOS device before executing the Ansible Playbook:
#show running-config ldp
!
!
#
Now we can execute the Ansible playbook and below are the logs that will be seen.
(ansible) osboxes@osboxes:~/playbooks$ ansible-playbook ldp-playbook.yml
PLAY [ocnos] ******************************************************************************************************
TASK [configure LDP config on OcNOS] ******************************************************************************
changed: [sw2]
PLAY RECAP ********************************************************************************************************
sw2 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Now check the configs on the OcNOS device. which should show the LDP configurations.
#show running-config ldp
!
router ldp
targeted-peer ipv4 1.1.1.1
exit-targeted-peer-mode
targeted-peer ipv4 3.3.3.3
exit-targeted-peer-mode
advertise-labels for only_lo to any
!
!
interface eth2
enable-ldp ipv4
!
interface eth1
enable-ldp ipv4
!
#
The below playbook shows how to check the runtime status of LDP protocol through the 'show ldp session' command and prints its output. It is assumed that the neighboring nodes are configured accordingly to get the LDP session up and running.
showldp-playbook.yml
osboxes@osboxes:~/playbooks$ cat showldp-playbook.yml
---
- hosts: ocnos
gather_facts: no
tasks:
- name: show LDP config ocnos
cli_command:
command: show ldp session
register: result
- name: debug
debug:
msg: "{{ result.stdout_lines }}"
osboxes@osboxes:~/playbooks$
When you run this playbook, the following will be its output. Parsing of the show command output needs to be done to determine if the runtime status of the protocol is fine.
(ansible) osboxes@osboxes:~/playbooks$ ansible-playbook showldp-playbook.yml
PLAY [ocnos] ******************************************************************************************************
TASK [show LDP config ocnos] **************************************************************************************
ok: [sw2]
TASK [debug] ******************************************************************************************************
ok: [sw2] => {
"msg": [
"Peer IP Address IF Name My Role State KeepAlive UpTime",
"3.3.3.3 eth2 Passive OPERATIONAL 30 03:58:20",
"1.1.1.1 eth1 Active OPERATIONAL 30 03:58:20"
]
}
PLAY RECAP ********************************************************************************************************
sw2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(ansible) osboxes@osboxes:~/playbooks$
The below playbook is to unconfigure the LDP configuration on the OcNOS device.
osboxes@osboxes:~/playbooks$ cat unconfigureldp-playbook.yml
---
- hosts: ocnos
gather_facts: no
tasks:
- name: give "no router ldp" command
ipinfusion.ocnos.ocnos_config:
lines:
- no router ldp
- name: show LDP config ocnos
cli_command:
command: show running-config ldp
register: result
- name: debug
debug:
msg: "{{ result.stdout_lines }}"