I am struggling to delete a DHCP config after comparing 2 configs using the Ansible difference feature. I have registered the error output in a variable at the end for better understanding. I am calling the first item
from a list in a yml file then the second item is the difference between the two configs which needs to be deleted.
My playbook tasks:
- name: Verify the Added DHCP Config on NXOS Devices nxos_command: commands: - show running-config dhcp | include Vlan|Ethernet|address register: post_output - name: View Data from the show run dhcp command debug: var: post_output.stdout_lines - name: Capture the Existing DHCP Server Config set_fact: existing_dhcp_config: "" - name: View the existing DHCP Server IP(s) debug: var: existing_dhcp_config - name: Desired DHCP Server Config Based on Site Standards set_fact: desired_dhcp_config: "-DHCP-Config/-dhcp.cfg') | regex_findall('interface \\S+|ip dhcp relay address \\S+') }}" - name: View Desired DHCP Server Config debug: var: desired_dhcp_config - name: Compare Existing and Desired Configs to Remove Stale Config set_fact: dhcp_config_to_remove: "" - name: View the Difference Against Existing and Desired DHCP Configs debug: var: dhcp_config_to_remove - name: Delete Non-Site Standards DHCP Config nxos_config: parents: interface commands: no diff_against: running save_when: modified loop: - "" - "" register: var ignore_errors: yes - debug: var: var
Playbook run output:
========================= Running DHCP/IP Helper Add Playbook ========================= : ok: [nxos-1] TASK [Verify the Added DHCP Config on NXOS Devices] ****************************************************************************************************************************************************************************************************************************************************************************************************************************** ok: [nxos-1] TASK [View Data from the show run dhcp command] ********************************************************************************************************************************************************************************************************************************************************************************************************************************** ok: [nxos-1] => { "post_output.stdout_lines": [ [ "interface Vlan1", " ip dhcp relay address 10.1.1.2 ", " ip dhcp relay address 10.5.5.98 ", " ip dhcp relay address 10.5.5.99 ", "interface Ethernet1/49", " ip dhcp relay address 10.1.1.2 ", " ip dhcp relay address 10.5.5.98 ", " ip dhcp relay address 10.5.5.99 ", "interface Ethernet1/50", " ip dhcp relay address 10.1.1.2 ", " ip dhcp relay address 10.5.5.98 ", " ip dhcp relay address 10.5.5.99" ] ] } TASK [Capture the Existing DHCP Server Config] *********************************************************************************************************************************************************************************************************************************************************************************************************************************** ok: [nxos-1] TASK [View the existing DHCP Server IP(s)] *************************************************************************************************************************************************************************************************************************************************************************************************************************************** ok: [nxos-1] => { "existing_dhcp_config": [ "interface Vlan1", "ip dhcp relay address 10.1.1.2", "ip dhcp relay address 10.5.5.98", "ip dhcp relay address 10.5.5.99", "interface Ethernet1/49", "ip dhcp relay address 10.1.1.2", "ip dhcp relay address 10.5.5.98", "ip dhcp relay address 10.5.5.99", "interface Ethernet1/50", "ip dhcp relay address 10.1.1.2", "ip dhcp relay address 10.5.5.98", "ip dhcp relay address 10.5.5.99" ] } TASK [Desired DHCP Server Config Based on Site Standards] ************************************************************************************************************************************************************************************************************************************************************************************************************************ ok: [nxos-1] TASK [View Desired DHCP Server Config] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************* ok: [nxos-1] => { "desired_dhcp_config": [ "interface Vlan1", "ip dhcp relay address 10.1.1.2", "interface Ethernet1/49", "ip dhcp relay address 10.1.1.2", "interface Ethernet1/50", "ip dhcp relay address 10.1.1.2" ] } TASK [Compare Existing and Desired Configs to Remove Stale Config] *************************************************************************************************************************************************************************************************************************************************************************************************************** ok: [nxos-1] TASK [View the Difference Against Existing and Desired DHCP Configs] ************************************************************************************************************************************************************************************************************************************************************************************************************* ok: [nxos-1] => { "dhcp_config_to_remove": [ "ip dhcp relay address 10.5.5.98", "ip dhcp relay address 10.5.5.99" ] } TASK [Delete Non-Site Standards DHCP Config] ************************************************************************************************************************************************************************************************************************************************************************************************************************************* failed: [nxos-1] (item=['Vlan1', 'Ethernet1/49 - 50']) => {"ansible_loop_var": "item", "changed": false, "item": ["Vlan1", "Ethernet1/49 - 50"], "msg": "interface ['Vlan1'\r\r\n ^\r\nInvalid interface format at '^' marker.\r\n\rnxos-1(config)# "} failed: [nxos-1] (item=['ip dhcp relay address 10.5.0.98', 'ip dhcp relay address 10.5.0.99']) => {"ansible_loop_var": "item", "changed": false, "item": ["ip dhcp relay address 10.5.0.98", "ip dhcp relay address 10.5.0.99"], "msg": "interface ['ip dhcp relay address 10.5.0.98'\r\r\n ^\r\nInvalid interface format at '^' marker.\r\n\rnxos-1(config)# "} ...ignoring TASK [debug] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* ok: [nxos-1] => { "var": { "changed": false, "failed": true, "msg": "All items completed", "results": [ { "ansible_loop_var": "item", "changed": false, "failed": true, "invocation": { "module_args": { "after": null, "auth_pass": null, "authorize": null, "backup": false, "backup_options": null, "before": null, "commands": "no ['Vlan1', 'Ethernet1/49 - 50']", "defaults": false, "diff_against": "running", "diff_ignore_lines": null, "host": null, "intended_config": null, "lines": [ "no ['Vlan1'", " 'Ethernet1/49 - 50']" ], "match": "line", "parents": [ "interface ['Vlan1'", " 'Ethernet1/49 - 50']" ], "password": null, "port": null, "provider": null, "replace": "line", "replace_src": null, "running_config": null, "save_when": "modified", "src": null, "ssh_keyfile": null, "timeout": null, "transport": null, "use_ssl": null, "username": null, "validate_certs": null } }, "item": [ "Vlan1", "Ethernet1/49 - 50" ], "msg": "interface ['Vlan1'\r\r\n ^\r\nInvalid interface format at '^' marker.\r\n\rnxos-1(config)# " }, { "ansible_loop_var": "item", "changed": false, "failed": true, "invocation": { "module_args": { "after": null, "auth_pass": null, "authorize": null, "backup": false, "backup_options": null, "before": null, "commands": "no ['ip dhcp relay address 10.5.0.98', 'ip dhcp relay address 10.5.0.99']", "defaults": false, "diff_against": "running", "diff_ignore_lines": null, "host": null, "intended_config": null, "lines": [ "no ['ip dhcp relay address 10.5.0.98'", " 'ip dhcp relay address 10.5.0.99']" ], "match": "line", "parents": [ "interface ['ip dhcp relay address 10.5.0.98'", " 'ip dhcp relay address 10.5.0.99']" ], "password": null, "port": null, "provider": null, "replace": "line", "replace_src": null, "running_config": null, "save_when": "modified", "src": null, "ssh_keyfile": null, "timeout": null, "transport": null, "use_ssl": null, "username": null, "validate_certs": null } }, "item": [ "ip dhcp relay address 10.5.0.98", "ip dhcp relay address 10.5.0.99" ], "msg": "interface ['ip dhcp relay address 10.5.0.98'\r\r\n ^\r\nInvalid interface format at '^' marker.\r\n\rnxos-1(config)# " } ] } } PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** nxos-1 : ok=11 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
group_vars/nxos.yml:
--- interfaces: ["Vlan1", "Ethernet1/49 - 50"]
https://stackoverflow.com/questions/65535036/how-to-delete-a-config-after-comparing-and-getting-the-difference-in-an-ansible January 02, 2021 at 08:37AM
没有评论:
发表评论