cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1941
Views
0
Helpful
6
Replies

How to properly use the value of an existing node in `foreach` in a device template

juacosta
Cisco Employee
Cisco Employee

I want to remove all dns-servers in an ios device. I created the following template attempting to remove the existing DNS servers

 

<config xmlns="http://tail-f.com/ns/config/1.0">
  <devices xmlns="http://tail-f.com/ns/ncs">
  <device>
    <name>{$DEVICE_NAME}</name>
      <config>
      <ip xmlns="urn:ios">
      <dhcp>
        <pool>
          <id>LAN-POOL</id>
          <?foreach {dns-server}?>
             <dns-server tags="delete">{dns-server}</dns-server>
          <?end?>
        </pool>
      </dhcp>
      </ip>
      </config>
  </device>
  </devices>
</config>

NSO process the template successfully but the config remains. It seems the way I am dereferencing the value of the dns-server node it is not correct.

 

I attempted a variation, I made {dns-server} to be {/dns-server} and "/dns-server" was interpreted as a value and it looks like a new entry will be added when I do a sh template

[edit]
admin@ncs% show devices template BBB
config {
    ios:ip {
        dhcp {
            pool LAN-POOL {
                dns-server [ "{/dns-server}" ];
            }
        }
    }
}
[ok][2019-05-10 13:31:17]

[edit]
admin@ncs%

This is the original config I want to remove <dns-server> entries from

admin@ncs% show devices device CPE-c2d09c6c-6688-4b3f-b74a-f9fb18be6326 config ios:ip dhcp pool | display xml
<config xmlns="http://tail-f.com/ns/config/1.0">
  <devices xmlns="http://tail-f.com/ns/ncs" tags=" merge ">
  <device>
    <name>CPE-c2d09c6c-6688-4b3f-b74a-f9fb18be6326</name>
      <config>
      <ip xmlns="urn:ios">
      <dhcp>
      <pool refcounter="2"  backpointer="[ /vmsservice:ufp/vmsservice:vmsservice[vmsservice:name='CPE-c2d09c6c-6688-4b3f-b74a-f9fb18be6326'] ]" >
        <id>LAN-POOL</id>
        <network>
          <network-number>172.26.0.0</network-number>
        </network>
        <dns-server>172.16.1.102</dns-server>
        <dns-server>172.16.2.103</dns-server>
        <dns-server>172.16.2.104</dns-server>
        <domain-name>msx.cisco.com</domain-name>
      </pool>
      </dhcp>
      </ip>
      </config>
  </device>
  </devices>
</config>
[ok][2019-05-10 14:04:15]

[edit]
admin@ncs%

Any thoughts ?

6 Replies 6

vleijon
Cisco Employee
Cisco Employee

This looks like an attempt to delete a list of dns-servers that you define in your service model. But it sounds like you actually want to delete all pre-existing dns-servers and that is actually much easier.

 

A tempalte to delete all dns-servers looks something like this:

       <ip xmlns="urn:ios">
         <dhcp>
           <pool>
             <id>LAN-POOL</id>
             <dns-server tags="delete"></dns-server>
           </pool>
         </dhcp>
       </ip>

@vleijon Thanks a lot for the response. I tried and that fails with this error

 

y error: <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
  <rpc-error>
    <error-type>application</error-type>
    <error-tag>operation-failed</error-tag>
    <error-severity>error</error-severity>
    <error-message unknown:lang="en">Exception in callback: Exception while applying template to device CPE-c2d09c6c-6688-4b3f-b74a-f9fb18be6326 message /ncs:devices/template{RemovingDNSConfigs}/config/ip/dhcp/pool{LAN-POOL}/dns-server{""}: Expression '' resulted in an incompatible value '' for /ncs:devices/device{CPE-c2d09c6c-6688-4b3f-b74a-f9fb18be6326}/config/ip/dhcp/pool{LAN-POOL}/dns-server</error-message>
  </rpc-error>
</rpc-reply>

    at com.cisco.vms.orchestrationservice.nso.NSOClient.handleEditConfig(NSOClient.java:219)
    at com.cisco.vms.orchestrationservice.stream.handler.OrchestrationMessageProcessor.submitRequest(Orchestration

Running on NSO 4.7.1.

 

I think it fails because the XPATH query will not be able to find a match. I tried the abbreviated form before trying to use foreach with  

<dns-server tags="delete" />

There is something weird going on there, try doing | debug template and sharing the template itself and we might be able to figure it out.

This is the template I am using

 

<config xmlns="http://tail-f.com/ns/config/1.0">
  <devices xmlns="http://tail-f.com/ns/ncs">
  <device>
    <name>{$DEVICE_NAME}</name>
      <config>
      <ip xmlns="urn:ios">
      <dhcp>
        <pool>
          <id>LAN-POOL</id>
          <dns-server tags="delete"></dns-server>
        </pool>
      </dhcp>
      </ip>
      </config>
  </device>
  </devices>
</config>

Sorry, I have no idea why the template doesn't work for you even tough it does for you. You can try doing your commit | debug template  or | debug service to try and find more information,

The best debug information I was able to retrieve is this

 

[edit]
admin@ncs% request devices device CPE-4a07e720-f89f-447b-8a0d-5cf4b3c46542 apply-template template-name Clean\ Previous\ DNS\ Servers | debug template
Operation 'merge' on existing node: /devices/device[name='CPE-4a07e720-f89f-447b-8a0d-5cf4b3c46542']
Fetching literal "LAN-POOL"
Operation 'merge' on existing node: /devices/device[name='CPE-4a07e720-f89f-447b-8a0d-5cf4b3c46542']/config/ios:ip/dhcp/pool[id='LAN-POOL']
Fetching literal []
Error: 'devices template "Clean Previous DNS Servers" config ip dhcp pool LAN-POOL dns-server ""': Expression '' resulted in an incompatible value '' for 'devices device CPE-4a07e720-f89f-447b-8a0d-5cf4b3c46542 config ip dhcp pool LAN-POOL dns-server'
[error][2019-05-10 22:20:14]

[edit]
admin@ncs% 

 

I think the Fetching Literal [] confirms XPATH is looking for non empty value in the <dns-server tags=delete></dns-server>

Polls
AI-powered tools for network troubleshooting are likely to be part of everyone’s workflow sooner or later. What is the single biggest challenge or concern you see with adopting these tools in your organization?