cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
373
Views
5
Helpful
6
Replies
Highlighted

Unable to use XPath as a value when deploying a device template with variables

We are using NSO 5.3.1.1

admin@8a4d49ee4c2a# show ncs-state version
ncs-state version 5.3.1.1

We attempted to apply a device template with a variable. This works fine. When doing it, we noticed that <node set> was an available option, which is an XPath expression.

admin@8a4d49ee4c2a(config)# devices device csr1 apply-template template-name test-variables variable { name NAME value ?
Possible completions:
<node set> an XPath expression resulting in a node set
<number> some integer or float number. This is an XPath number.
<quoted> some text enclosed in single quotes. This is an XPath string.

While attempting to use the results from an XPath expression in the device template (similar to how we would for a service template), we were unable to actually get it to work.

admin@8a4d49ee4c2a(config)# devices device csr1 apply-template template-name test-variables variable { name NAME value /devices/device[name='csr1']/address }
Error: Variable: NAME could not be set to: /devices/device[name='csr1']/address due to: XPATH syntax error. Did you forget enclosing single quotes for string?

admin@8a4d49ee4c2a(config)# devices device csr1 apply-template template-name test-variables variable { name NAME value /devices/device/address }
Error: Variable: NAME could not be set to: /devices/device/address due to: XPATH syntax error. Did you forget enclosing single quotes for string?

 

We attempted to use a XPath function of string(), thinking that may be required. But it failed, doing a regular xpath eval with string() does provide a value though.

admin@8a4d49ee4c2a(config)# devices device csr1 apply-template template-name test-variables variable { name NAME value string(/devices/device/address) }
Error: Variable: NAME could not be set to: string(/devices/device/address) due to: XPATH syntax error. Did you forget enclosing single quotes for string?

admin@8a4d49ee4c2a(config)# xpath eval string(/devices/device/address)
10.11.12.113

admin@8a4d49ee4c2a(config)# xpath eval /devices/device/address
/devices/device[name='csr-lab']/address :: 10.11.12.113
/devices/device[name='csr1']/address :: 10.11.12.113
/devices/device[name='gns3-csr1']/address :: 172.16.233.168
/devices/device[name='gns3-iosv-1']/address :: 172.16.233.167
/devices/device[name='gns3-ospf-iosv1']/address :: 172.16.233.216
/devices/device[name='gns3-ospf-iosv2']/address :: 172.16.233.217
/devices/device[name='new']/address :: 172.16.233.168

 

We used a XPath function, and found that it is being evaluated.

allen.chen@nso-hq-1(config)# devices device-group mygroup apply-template template-name vartest variable { name NAME value count() }
Error: Variable: NAME could not be set to: count() due to: Function count() can not be called with 0 arguments. Did you forget enclosing single quotes for string?

Below is the YANG, it looks like the type is yang:xpath1.0

list variable {
key name;
leaf name {
tailf:cli-completion-actionpoint "template-variable";
type string {
tailf:info
"The name of the XPath variable as used in the template";
}
mandatory "true";
}
leaf value {
type union {
type string {
tailf:info
"quoted;;some text enclosed in single quotes. This
is an XPath string.";
}
type string {
tailf:info
"number;;some integer or float number. This is an XPath
number.";
}
type yang:xpath1.0 {
tailf:info
"node set;;an XPath expression resulting in a node set";
}
}
mandatory "true";
}
}

Does anyone have any idea what we are missing? Or is this bug?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

From opening a Cisco TAC case and getting information from the developers, the <node path> is not supported and the documentation is currently incorrect.

 

Hi Samuel,

I checked with our developer, currently we do not support <node-set> in apply-template with variables. I will log an internal ticket to correct the data model or provides a better error message that tells that we don't accept XPath expressions as valid values.
In the meantime the only option is the workaround mentioned

Thanks
Brad Moseley
Cisco Systems

View solution in original post

6 REPLIES 6
Highlighted
Beginner

I have the same issue. Trying to use XPath on template variables to point to XPATH node sets, but I get the same error message all the time, same error message.

Highlighted
Beginner

As an alternative to passing XPATH variables to templates, you could do the following:

 

1. Define YANG model to store your baseline configs:

 

module provisioning {
namespace "http://com/example/provisioning";
prefix provisioning;

import ietf-inet-types {
prefix inet;
}
import tailf-ncs {
prefix ncs;
}
import tailf-common {
prefix tailf;
}

container provisioning {
list base-config {
key device;

uses ncs:service-data;
ncs:servicepoint "provisioning";

leaf device {
tailf:info "Device";
type leafref {
path "/ncs:devices/ncs:device/ncs:name";
}
}

leaf address {
type inet:ipv4-address;
}
}
}
}  

 

2. Use NSO to populate the provisioning config:

 

admin@ncs% show provisioning
base-config csr1 {
address 10.11.12.113;
}

3. Define template to use the provisioning values via XPATH expressions

 

admin@ncs% show devices template Template_provisioning_example | display set
set devices templateTemplate_provisioning_example <your_path_to_address_leaf> address "{/provisioning/base-config[device=$DEVICE]/address}"

 4. Apply template without passing variables, since now everything is available in CDB to be accessible dynamically via XPATH:

 

admin@ncs% request devices device csr1 apply-template template-name Template_provisioning_example

 

 

The template would pick up csr1 from $DEVICE variable and look for the right configuration under /provisioning XPATH tree

Highlighted

Thanks, but I'm not looking for a work around. I was hoping to learn if/how to use the build in xpath for the device templates.

Highlighted
Cisco Employee

Hello,

Quoting from the documentation (NSO Development Guide / Templates / Basic Principles / Values in a template)

If the value contains a pair of {...} the string between the curly braces is 
treated as an XPath 1.0 expression.

So, you would do

devices device csr1 apply-template template-name test-variables 
variable { name NAME value "{/devices/device[name='csr1']/address}" }

Note the "{...}" around the Xpath expression.

/Ram

PS: The Templates chapter in the Development guide is quite a good read, with lots of examples of advanced usage.

Highlighted

Hi Ram,

 

Have you tried this? Did you get it to work?

I ask because when I have tried it, it fails. 
I gave it a try after gusmb responded with his example, which had xpath inside of quotes and curlies.

 

admin@ncs% show devices template Template_provisioning_example | display set
set devices templateTemplate_provisioning_example <your_path_to_address_leaf> address "{/provisioning/base-config[device=$DEVICE]/address}"


However when I try it, it fails.

 

The first example uses double quotes, the second does not. The interesting thing, is if I tab complete, it removes the double quotes.

 

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value "{/devices/device[name='csr1']/name}" }
Error: Variable: NAME could not be set to: {/devices/device[name='csr1']/name} due to: Invalid name: {/devi. Did you forget enclosing single quotes for string?

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value {/devices/device[name='csr1']/name} }
Error: Variable: NAME could not be set to: {/devices/device[name='csr1']/name} due to: Invalid name: {/devi. Did you forget enclosing single quotes for string?

samuel.coome@nso-hq-1(config)#

I also attempted it without a xpath variable, incase that was causing an issue (thought then it'd be a list returned instead of a single value...).

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value "{/devices/device/name}" }
Error: Variable: NAME could not be set to: {/devices/device/name} due to: Invalid name: {/devi. Did you forget enclosing single quotes for string?

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value {/devices/device/name} }
Error: Variable: NAME could not be set to: {/devices/device/name} due to: Invalid name: {/devi. Did you forget enclosing single quotes for string?

So no quotes, and double quotes don't seem to work with {...}, so I tried single quotes.

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value '{/devices/device[name=\'csr1\']/name}' }
Error: Variable: NAME could not be set to: '{/devices/device[name='csr1']/name}' due to: XPath syntax error. Did you forget enclosing single quotes for string?

samuel.coome@nso-hq-1(config)# devices device csr1 apply-template template-name vartest variable { name NAME value '{/devices/device[name="csr1"]/name}' }
apply-template-result {
    device csr1
    result ok
}

samuel.coome@nso-hq-1(config)# commit dry-run
cli {
    local-node {
        data  devices {
                  device csr1 {
                      config {
             -            ios:hostname csr1;
             +            ios:hostname "{/devices/device[name=\"csr1\"]/name}";
                      }
                  }
              }
    }
}

Which, as you can see ends up just taking it as a string.

 

I'm trying to get a working example of the XPath actually working for a device-template value. If you can demonstrate a working example, perhaps I will understand better, but so far the documentation doesn't seem correct, or I do not understand it.

I have opened a TAC case looking for more help.

 

 

Highlighted

From opening a Cisco TAC case and getting information from the developers, the <node path> is not supported and the documentation is currently incorrect.

 

Hi Samuel,

I checked with our developer, currently we do not support <node-set> in apply-template with variables. I will log an internal ticket to correct the data model or provides a better error message that tells that we don't accept XPath expressions as valid values.
In the meantime the only option is the workaround mentioned

Thanks
Brad Moseley
Cisco Systems

View solution in original post