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”**
**    ]**
}