Skip to content

Fix: Preserve AS-DOT ASN string formatting in ansible_collections utils.Template() #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kolbashj
Copy link

@kolbashj kolbashj commented May 9, 2025

SUMMARY
  • BGP ASNs using asdot notation and ending with trailing zeroes 65000.1000 are being misrepresented. Trailing zeroes are being stripped, returning 65000.1
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME
  • ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils
  • Template().call method
ADDITIONAL INFORMATION

I work with the cisco_ios network module a lot. When trying to gather bgp_global facts the 'as_number' is truncated. Traced the bug back to the above-mentioned module. The value loses its trailing zeroes after being processed by ast.literal_eval() before being returned.

BEFORE CHANGE
PLAY [CISCO FACTS] *****************************************************************************************************************************************************************************************************************

TASK [cisco_facts : CISCO IOS - GATHER FACTS] **************************************************************************************************************************************************************************************
Thursday 08 May 2025  23:23:15 -0700 (0:00:00.008)       0:00:00.008 ********** 
Thursday 08 May 2025  23:23:15 -0700 (0:00:00.007)       0:00:00.007 ********** 
ok: [edge1-rt]

TASK [cisco_facts : CISCO IOS - PRINT FACTS] ***************************************************************************************************************************************************************************************
Thursday 08 May 2025  23:23:17 -0700 (0:00:02.043)       0:00:02.051 ********** 
Thursday 08 May 2025  23:23:17 -0700 (0:00:02.043)       0:00:02.051 ********** 
ok: [edge1-rt] => 
  ios_facts:
    ansible_facts:
      ansible_net_api: cliconf
      ansible_net_gather_network_resources:
      - bgp_global
      ansible_net_gather_subset:
      - default
      ansible_net_hostname: edge1-rt
      ansible_net_image: bootflash:packages.conf
      ansible_net_iostype: IOS-XE
      ansible_net_model: C8000V
      ansible_net_operatingmode: autonomous
      ansible_net_python_version: 3.12.3
      ansible_net_system: ios
      ansible_net_version: 17.12.04a
      ansible_network_resources:
        bgp_global:
          aggregate_addresses:
          - address: 205.85.0.0
            netmask: 255.255.248.0
          as_number: '65000.1'
 
TRUNCATED OUTPUT ---

PLAY RECAP *************************************************************************************************************************************************************************************************************************
edge1-rt                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds
Thursday 08 May 2025  23:23:17 -0700 (0:00:00.038)       0:00:02.089 ********** 
=============================================================================== 
cisco_facts : CISCO IOS - GATHER FACTS -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2.04s
cisco_facts : CISCO IOS - PRINT FACTS --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.04s
Thursday 08 May 2025  23:23:17 -0700 (0:00:00.038)       0:00:02.089 ********** 
=============================================================================== 
cisco_facts ------------------------------------------------------------- 2.08s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
total ------------------------------------------------------------------- 2.08s
AFTER CHANGE
PLAY [CISCO FACTS] *****************************************************************************************************************************************************************************************************************

TASK [cisco_facts : CISCO IOS - GATHER FACTS] **************************************************************************************************************************************************************************************
Thursday 08 May 2025  23:28:40 -0700 (0:00:00.008)       0:00:00.008 ********** 
Thursday 08 May 2025  23:28:40 -0700 (0:00:00.008)       0:00:00.008 ********** 
ok: [edge1-rt]

TASK [cisco_facts : CISCO IOS - PRINT FACTS] ***************************************************************************************************************************************************************************************
Thursday 08 May 2025  23:28:42 -0700 (0:00:02.022)       0:00:02.030 ********** 
Thursday 08 May 2025  23:28:42 -0700 (0:00:02.022)       0:00:02.030 ********** 
ok: [edge1-rt] => 
  ios_facts:
    ansible_facts:
      ansible_net_api: cliconf
      ansible_net_gather_network_resources:
      - bgp_global
      ansible_net_gather_subset:
      - default
      ansible_net_hostname: edge1-rt
      ansible_net_image: bootflash:packages.conf
      ansible_net_iostype: IOS-XE
      ansible_net_model: C8000V
      ansible_net_operatingmode: autonomous
      ansible_net_python_version: 3.12.3
      ansible_net_system: ios
      ansible_net_version: 17.12.04a
      ansible_network_resources:
        bgp_global:
          aggregate_addresses:
          - address: 205.85.0.0
            netmask: 255.255.248.0
          as_number: '65000.1000'

TRUNCATED OUTPUT ---

PLAY RECAP *************************************************************************************************************************************************************************************************************************
edge1-rt                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds
Thursday 08 May 2025  23:28:42 -0700 (0:00:00.037)       0:00:02.068 ********** 
=============================================================================== 
cisco_facts : CISCO IOS - GATHER FACTS -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2.02s
cisco_facts : CISCO IOS - PRINT FACTS --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.04s
Thursday 08 May 2025  23:28:42 -0700 (0:00:00.037)       0:00:02.067 ********** 
=============================================================================== 
cisco_facts ------------------------------------------------------------- 2.06s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
total ------------------------------------------------------------------- 2.06s

@kolbashj
Copy link
Author

kolbashj commented Jun 3, 2025

Hi team 👋 — just following up on this PR.

This patch addresses a parsing bug where valid AS-DOT formatted ASNs (e.g. 65000.1000) are incorrectly coerced to floats (65000.1) during template evaluation inside network_template._template() due to the use of ast.literal_eval(). This breaks downstream usage of parsed bgp_global facts, especially when re-rendering configs or comparing state.

I've included:

A regex-safe solution that preserves AS-DOT values as strings

Unit tests to verify the behavior

A scoped implementation that avoids impact on unrelated values

Let me know if any changes or refinements are needed — happy to iterate.

Thanks for reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant