Cisco IOS collection unable to display config diff

Hello all, I’m new to using Ansible, and this forum so please let me know if I’m doing something wrong here. The results below are from a lab I have set up to develop my companies Ansible capabilities.

The goal:
To use the base-config.cfg file (expressed as the variable master_config) to use as a reference point to ensure the lab switches are running the exact same config.

This has been accomplished! This totally works and it’s repeatable (thank you chatgpt).

Where I’m struggling:
The only time I can consistently JUST see the diff is by using the --diff module when running the playbook. You’ll see below that caching the diff in register: config_diff and attempting to display it using debug: var: config_diff does not work.

The code itself is here at the top, while the output/ logs are at the bottom of this post.

The code

**- name: Standard SW Provisioning playbook**

**  hosts: switches**

**  gather_facts: no**

**  tasks:**

**  - name: Pre-load master config**
**    set_fact:**
**      master_config: "{{ lookup('file', 'master.cfg/base-config.cfg') }}"**

**  - name: Check running-config against master config**
**    cisco.ios.ios_config:**
**      diff_against: intended**
**      intended_config: "{{ master_config }}"**
**      match: line**
**    register: config_diff** 


**  - name: Display the configuration differences**
**    debug:**
**      var: config_diff**

**  - name: Apply master config when differences are found**
**    cisco.ios.ios_config:**
**      lines: "{{ master_config }}"**
**    when: config_diff is defined and config_diff != ''**

LOGS/ OUTPUT BELOW

This is the result from the var: config_diff line:
ok: [10.10.41.2] => {
** “config_diff”: {**
** “changed”: false,**
** “failed”: false**
** }**
}
ok: [10.10.41.3] => {
** “config_diff”: {**
** “changed”: false,**
** “failed”: false**
** }**
}

However, when the cisco.ios.ios_config collection runs, it detects the changes I’m making to the switches and accordingly changes the running config to match the base-config.cfg

changed: [redacted] => {
** “banners”: {},**
** “changed”: true,**
** “commands”: [**
** “hostname labcat1”**
** ],**
** “invocation”: {**
** “module_args”: {**
** “after”: null,**
** “backup”: false,**
** “backup_options”: null,**
** “before”: null,**
** “defaults”: false,**
** “diff_against”: null,**
** “diff_ignore_lines”: null,**
** “intended_config”: null,**
** “lines”: [**
** “hostname labcat1\ninterface GigabitEthernet1/0/1\n description hey over here\n!\ninterface Vlan10\n ip address 192.168.1.1 255.255.255.0\n!”**
** ],**
** “match”: “line”,**
** “multiline_delimiter”: “@”,**
** “parents”: null,**
** “replace”: “line”,**
** “running_config”: null,**
** “save_when”: “never”,**
** “src”: null**
** }**
** },**
** “updates”: [**
** “hostname labcat1”**
** ]**
}
changed: [redacted] => {
** “banners”: {},**
** “changed”: true,**
** “commands”: [**
** “hostname labcat1”**
** ],**
** “invocation”: {**
** “module_args”: {**
** “after”: null,**
** “backup”: false,**
** “backup_options”: null,**
** “before”: null,**
** “defaults”: false,**
** “diff_against”: null,**
** “diff_ignore_lines”: null,**
** “intended_config”: null,**
** “lines”: [**
** “hostname labcat1\ninterface GigabitEthernet1/0/1\n description hey over here\n!\ninterface Vlan10\n ip address 192.168.1.1 255.255.255.0\n!”**
** ],**
** “match”: “line”,**
** “multiline_delimiter”: “@”,**
** “parents”: null,**
** “replace”: “line”,**
** “running_config”: null,**
** “save_when”: “never”,**
** “src”: null**
** }**
** },**
** “updates”: [**
** “hostname labcat1”**
** ]**
}

Hi!

So first, based on the module docs the diff_against attribute only matters when the --diff flag is applied. So it sounds like what your seeing is expected. If you run that task without the --diff flag, it seems like it does nothing

Second, your when clause not quite doing what you want

  1. config_diff will always be defined at this point since the task where config_diff is registered always runs (theres no when clause or tags on the task that might cause it to be skipped)
  2. config_diff is a dictionary, and will never be equal to an empty string.

Im not sure what a good when clause looks like for this module since im not familiar with the output. But maybe something like

when: config_diff.commands is defined and (config_diff.commands | length) > 0
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.