Ansible VOSS Config Idempotency

It is useful to know when a task has made a change and it should be possible to re-run the same task again and no changes are made. Some commands sent to the switch may return a changed state even though the configuration has changed before. This can be the case when using abbreviated commands. There are also different ways to apply the same configuration and it is worth inspecting the running configuration to see the syntax of the command in there which might be different to what was sent.

For example, you can create a new VLAN with one line and then give the VLAN a name with another. If you do this the playbook will always come back as changed=1 even though it made no changes on subsequent plays. By checking the running config you can see that the two command lines are converted to a single line and also includes quotes around the VLAN name.

Altering the Jinga2 source file used as a template to match the expected result in the running config will change the behaviour. Running the playbook for the first time will change the config and state will be changed=1. Subsequent runs of the playbook will not change anything as the configuration is found in the running config with the exact same string and syntax. The state will be changed=0.

$ ansible-playbook voss_config_vlan.yml

PLAY [PLAY 1: Manage VLANs with voss_config and jinja2] ********************************************

TASK [TASK 1: Apply config via SSH] ****************************************************************

changed: [r1]

RUNNING HANDLER [HANDLER 1: Display changes] *******************************************************

ok: [r1] => {

“msg”: [

“vlan create 300 name \”Sales\” type port-mstprstp 0″,

“vlan create 400 name \”IT\” type port-mstprstp 0″

]

}

PLAY RECAP *****************************************************************************************

r1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

$ ansible-playbook voss_config_vlan.yml

PLAY [PLAY 1: Manage VLANs with voss_config and jinja2] ********************************************

TASK [TASK 1: Apply config via SSH] ****************************************************************

ok: [r1]

PLAY RECAP *****************************************************************************************

r1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

###

VLAN extract from running config:

vlan create 300 name “Sales” type port-mstprstp 0
vlan create 400 name “IT” type port-mstprstp 0

Jinga2 template:

$ cat templates/vlans.j2

{% for vlan in vlans %}

vlan create {{ vlan.vid }} name “{{ vlan.description }}” type port-mstprstp 0

{% endfor %}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s