PackeTsar / freeztp

An Open-Source Zero-Touch Provisioning System for Cisco IOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use the ippaddr variable in a template?

jasarria87 opened this issue · comments

I am trying to use the variable "ippaddr" that corresponds to the IP that I assign by DHCP in the switch provisioning.

My goal is to be able to take the first 3 octets of this IP, compare them with an "if" statement and then make some configurations in the template.

I have tried creating a variable using "keystore" and it does not work.

I have tried to enter the variable directly in the template and it doesn't work either.

Can you help me how would be the correct way?

Examples that I have done and they do not work:

!{% set ip_sw = {{ ipaddr }} %}
!{% if ip_sw[:9] == "172.31.21" %}
vlan 711
name EXAMPLE
!
!{% endif %}

====================================

ztp set keystore SW_2960_01 ip_sw '{{ ipaddr }}'
template...
interface vlan 1
ip address {{ ip_sw }} 255.255.255.255.0
no shut
!

Hi -

Looking at your example, you are incorrectly referencing the 'ipaddr' variable by using {{ ipaddr }}. You don't need the braces in the if statement. It's also unlikely that you need the variable reassignment.

This is what I would expect to see.

#############
ztp set template DEMO1 ^
!   {% if ipaddr[:9] == "172.31.21" %}
vlan 711
 name EXAMPLE
!   {% endif %}
!
interface vlan1
 ip address {{ ipaddr }} 255.255.255.0
 no shut
!
^
#############
ztp set idarray MY-NEW-SWITCH FJB11111111
ztp set association id MY-NEW-SWITCH template DEMO1

Creation of VLAN 711 is conditional on the DHCP IP being prefixed '172.31.21'. The template then converts VLAN 1 to a hard set IP using the existing DHCP address.

PSC

Hi,

Hello,

I have tried as you suggest, and when the template goes to be provisioned, it generates the following error and the ZTP service goes down.

The error mentions that the variable 'ipaddr' is unknown.

What should I do then to be able to capture in a variable the assigned DHCP IP and pass it to my template?

2022-03-15_12h55_47

Can you export ztp show log and post it here?

Hi -

Testing in the lab shows the variable to be empty. Based on the documentation, the ipaddr variable is populated for logging data. I suspect an order of operations issue in the code. I'm going to flag this as a bug. I'll see if I can figure out a workaround.

PSC

Lab Log for download of config file:

2022-03-15 11:29:14:   tracking_class.report: New transfer of (ZTP-265B156EE1-confg) from (10.0.0.103) detected
2022-03-15 11:29:14:   interceptor: Called. Checking the cache
2022-03-15 11:29:14:   file_cache.get: File (ZTP-265B156EE1-confg) requested by (10.0.0.103) not in cache
2022-03-15 11:29:14:   interceptor: Cache returned None. Running cfact.lookup procedure
2022-03-15 11:29:14:   cfact.lookup: Called. Checking filename (ZTP-265B156EE1-confg) and IP (10.0.0.103)
2022-03-15 11:29:14:   cfact.lookup: TempID is (ZTP-265B156EE1)
2022-03-15 11:29:14:   cfact.lookup: Current SNMP Requests: ['ZTP-265B156EE1']
2022-03-15 11:29:14:   cfact.lookup: Seeing the suffix in the filename and the TempID in the SNMPRequests
2022-03-15 11:29:14:   cfact.lookup: The SNMP request is showing as completed
2022-03-15 11:29:14:   cfact._default_lookup: Checking if a default-keystore is configured and ready...
2022-03-15 11:29:14:   cfact._default_lookup: A default keystore ID (DEFAULT_KEYSTORE) is configured
2022-03-15 11:29:14:   cfact.lookup: The target ID is NOT in the Keystore or in an IDArray, but a default is configured
2022-03-15 11:29:14:   interceptor: cfact.lookup returned (True)
2022-03-15 11:29:14:   interceptor: Returning ztp_dyn_file instantiated object
2022-03-15 11:29:14:   ztp_dyn_file: Instantiated as (<__main__.ztp_dyn_file instance at 0x7f45b91f3410>)
2022-03-15 11:29:14:   cfact.request: Called with filename (ZTP-265B156EE1-confg) and IP (10.0.0.103)
2022-03-15 11:29:14:   cfact.request: Filename (ZTP-265B156EE1-confg) does NOT match the configured initialfilename
2022-03-15 11:29:14:   cfact.request: Stripped filename to TempID (ZTP-265B156EE1)
2022-03-15 11:29:14:   cfact.request: Delaying keystore loopup for (1000) milliseconds
2022-03-15 11:29:15:   cfact.request: Seeing the suffix in the filename and the TempID in the SNMP Requests
2022-03-15 11:29:15:   cfact.request: SNMP Request says it has completed
2022-03-15 11:29:15:   cfact.request: SNMP request returned target ID ({u'WS_C3850_SERIAL_NUMBER': '', u'WS_C2960_SERIAL_NUMBER': 'FOC1728W0JK'})
2022-03-15 11:29:15:   cfact.get_keystore_id: Checking Keystores and IDArrays for ()
2022-03-15 11:29:15:   cfact.get_keystore_id: Checking Keystore names for ()
2022-03-15 11:29:15:   cfact.get_keystore_id: ID () not found in keystore names, checking local IDArrays
2022-03-15 11:29:15:   cfact.get_keystore_id: ID () not found in local IDArrays, checking external Keystore names
2022-03-15 11:29:15:   cfact.get_keystore_id: ID () not found in external Keystore names, checking external IDArrays
2022-03-15 11:29:15:   cfact.get_keystore_id: ID () not found in external IDArrays. ID Lookup failed!
2022-03-15 11:29:15:   cfact.get_keystore_id: Checking Keystores and IDArrays for (FOC1728W0JK)
2022-03-15 11:29:15:   cfact.get_keystore_id: Checking Keystore names for (FOC1728W0JK)
2022-03-15 11:29:15:   cfact.get_keystore_id: ID (FOC1728W0JK) not found in keystore names, checking local IDArrays
2022-03-15 11:29:15:   cfact.get_keystore_id: ID (FOC1728W0JK) not found in local IDArrays, checking external Keystore names
2022-03-15 11:29:15:   cfact.get_keystore_id: ID (FOC1728W0JK) not found in external Keystore names, checking external IDArrays
2022-03-15 11:29:15:   cfact.get_keystore_id: ID (FOC1728W0JK) not found in external IDArrays. ID Lookup failed!
2022-03-15 11:29:15:   cfact.request: Keystore ID Lookup returned (None)
2022-03-15 11:29:15:   cfact.request: SNMP request for (10.0.0.103) returned an unknown ID, checking for default-keystore
2022-03-15 11:29:15:   cfact._default_lookup: Checking if a default-keystore is configured and ready...
2022-03-15 11:29:15:   cfact._default_lookup: A default keystore ID (DEFAULT_KEYSTORE) is configured
2022-03-15 11:29:15:   cfact.request: default-keystore is configured. Returning default
2022-03-15 11:29:15:   cfact.get_template: Looking up association for identity (DEFAULT_KEYSTORE)
2022-03-15 11:29:15:   cfact.get_template: Default-template is pointing to (DEFAULT_TEMPLATE)
2022-03-15 11:29:15:   cfact.get_template: Template (DEFAULT_TEMPLATE) exists in local config. Returning
2022-03-15 11:29:15:   cfact._global_lookup: Checking if a global-keystore is configured and ready...
2022-03-15 11:29:15:   cfact._global_lookup: Global-keystore configured as none. Discarding
2022-03-15 11:29:15:   cfact._global_lookup: Checking if a global-keystore is configured and ready...
2022-03-15 11:29:15:   cfact._global_lookup: Global-keystore configured as none. Discarding
2022-03-15 11:29:15:   cfact.merge_final_config: Merging with values:
{
    "hostname": "UNKNOWN_HOST", 
    "snmpinfo": {
        "WS_C3850_SERIAL_NUMBER": "", 
        "WS_C2960_SERIAL_NUMBER": "FOC1728W0JK", 
        "matched": null
    }
}
2022-03-15 11:29:15:   cfact.request: Returning the below config to TFTPy:
#########################
hostname UNKNOWN_HOST
!
interface Vlan1
 description 
 ip address dhcp
 no shut
!
ip default-gateway 10.0.0.1
!
ip domain-name ztplab.c1engineering.com
!
crypto key generate rsa modulus 2048
!
username admin privilege 15 secret admin
!
aaa new-model
!
!
aaa authentication login CONSOLE local
aaa authorization console
aaa authorization exec default local if-authenticated
!
!
ip ssh version 2
!
line vty 0 15
login authentication default
transport input all
line console 0
login authentication CONSOLE
end
#########################
2022-03-15 11:29:15:   integration_main.send: Starting initialization thread to wait for files
2022-03-15 11:29:15:   ztp_dyn_file: File size is 506 bytes
2022-03-15 11:29:15:   file_cache.store: Storing {1647368955.351053: {'ipaddr': '10.0.0.103', 'file': <__main__.ztp_dyn_file instance at 0x7f45b91f3410>, 'filename': 'ZTP-265B156EE1-confg'}}
2022-03-15 11:29:15:   ztp_dyn_file.close: Called. File closing.
2022-03-15 11:29:15:   interceptor: Called. Checking the cache
2022-03-15 11:29:15:   file_cache.get: File (ZTP-265B156EE1-confg) requested by (10.0.0.103) found in cache. Checking timeout.
2022-03-15 11:29:15:   file_cache.get: Cached file is still within timeout period. Resetting to 0 and returning
2022-03-15 11:29:15:   interceptor: Called. Checking the cache
2022-03-15 11:29:15:   file_cache.get: File (ZTP-265B156EE1-confg) requested by (10.0.0.103) found in cache. Checking timeout.
2022-03-15 11:29:15:   file_cache.get: Cached file is still within timeout period. Resetting to 0 and returning
2022-03-15 11:29:15:   interceptor: Called. Checking the cache
2022-03-15 11:29:15:   file_cache.get: File (ZTP-265B156EE1-confg) requested by (10.0.0.103) found in cache. Checking timeout.
2022-03-15 11:29:15:   file_cache.get: Cached file is still within timeout period. Resetting to 0 and returning
2022-03-15 11:29:25:   file_cache._maintain_cache: Timing out and deleting 1647368955.35:{'ipaddr': '10.0.0.103', 'file': <__main__.ztp_dyn_file instance at 0x7f45b91f3410>, 'filename': 'ZTP-265B156EE1-confg'}

Lab Merge Test (Note missing variable message):

root@ont-ztplab-demo:~# ztp request merge-test DEFAULT_KEYSTORE 
2022-03-15 11:36:40:   cfact.get_keystore_id: Checking Keystores and IDArrays for (DEFAULT_KEYSTORE)

2022-03-15 11:36:40:   cfact.get_keystore_id: Checking Keystore names for (DEFAULT_KEYSTORE)

2022-03-15 11:36:40:   cfact.get_keystore_id: ID (DEFAULT_KEYSTORE) resolved directly to a keystore

2022-03-15 11:36:40:   cfact.get_template: Looking up association for identity (DEFAULT_KEYSTORE)

2022-03-15 11:36:40:   cfact.get_template: Default-template is pointing to (DEFAULT_TEMPLATE)

2022-03-15 11:36:40:   cfact.get_template: Template (DEFAULT_TEMPLATE) exists in local config. Returning


Some variables in jinja template do not exist in keystore:
        -ipaddr


2022-03-15 11:36:40:   cfact._global_lookup: Checking if a global-keystore is configured and ready...

2022-03-15 11:36:40:   cfact._global_lookup: Global-keystore configured as none. Discarding

2022-03-15 11:36:40:   cfact.merge_test: Merging with values:
{
    "hostname": "UNKNOWN_HOST", 
    "snmpinfo": {
        "WS_C3850_SERIAL_NUMBER": "WS_C3850_SERIAL_NUMBER_FAKESERIAL", 
        "WS_C2960_SERIAL_NUMBER": "WS_C2960_SERIAL_NUMBER_FAKESERIAL", 
        "matched": "FAKEMATCHEDSERIAL"
    }
}

##############################
hostname UNKNOWN_HOST
!
interface Vlan1
 description 
 ip address dhcp
 no shut
!
ip default-gateway 10.0.0.1
!
ip domain-name ztplab.c1engineering.com
!
crypto key generate rsa modulus 2048
!
username admin privilege 15 secret admin
!
aaa new-model
!
!
aaa authentication login CONSOLE local
aaa authorization console
aaa authorization exec default local if-authenticated
!
!
ip ssh version 2
!
line vty 0 15
login authentication default
transport input all
line console 0
login authentication CONSOLE
end
##############################

@pschapman from the documentation, it looks like ipaddr is populated for a filepath merge, not a template merge. Is the desire here to be able to use that data in a template merge as well?

@PackeTsar - Yes. Requester wants to apply logic based on the IP prefix received from DHCP. I'll re-mark this as an enhancement request.

I updated the lab ZTP box DEFAULT_TEMPLATE with description {{ ipaddr} for interface Vlan1.

Do you mean that currently there is no possibility to capture the variable "ipaddr" to operate with some logic in the middle of a template?

@jasarria87 that's correct. But it shouldn't be difficult for me to add it in

Afraid so. Using the temporary IP on VLAN 1 as a permanent address is not a typical configuration. Typically much of the configuration data is pre-mapped either directly through keystore commands or via external keystore.

#############
ztp set template DEMO2^
!   {% if mgmt_addr[:8] == "192.0.2." %}
vlan 711
 name EXAMPLE
!   {% endif %}
!
interface vlan1
 ip address {{ mgmt_addr }} {{ mgmt_mask }}
 no shut
!
^
#############

ztp set idarray MY-NEW-SWITCH FJB11111111
ztp set keystore MY-NEW-SWITCH mgmt_addr 192.0.2.21
ztp set keystore MY-NEW-SWITCH mgmt_mask 255.255.255.0
ztp set association id MY-NEW-SWITCH template DEMO1

Thank you very much for your quick responses @PackeTsar and @pschapman.

@PackeTsar stayed tuned for when it is possible to add the "ipaddr" variable in a template.

In the same way, congratulate them and thank them for a great tool.