cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
109
Views
0
Helpful
1
Replies

Storing the hostname of the device into the variable from the yamlfile

o1
Cisco Employee
Cisco Employee

Hi Team,

In the n9k yaml file, I have devices section like this:

devices:
      device_1:

      device_2:

 

In the script, I call this object as :

self.parameters['uut'] = self.parameters['testbed'].devices['device_1']

I need to hardcode the hostname of the device in the script. Is there anyway, I can skip the hardcoding?

I tried : 

self.parameters['uut'] = self.parameters['testbed'].devices         -    (only had one device in yaml)

OR

self.parameters['uut'] = self.parameters['testbed'].devices[0].     - first device from devices block.

But both of these given error.

 

 

 

1 Accepted Solution

Accepted Solutions

So neither self.parameters['testbed'].devices nor self.parameters['testbed'].devices[0] will work directly because the devices section is a dictionary with device names as keys. You could modify your script to get the first device's hostname, with something like this (Keep in mind that this solution assumes that the order of devices in the YAML file is consistent and that you always want to access the first device)
 
device_keys = list(self.parameters['testbed'].devices.keys())
first_device_name = device_keys[0]
self.parameters['uut'] = self.parameters['testbed'].devices[first_device_name]
hostname = self.parameters['uut'].hostname  # assuming there is a 'hostname' attribute in your device object

 Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

1 Reply 1

So neither self.parameters['testbed'].devices nor self.parameters['testbed'].devices[0] will work directly because the devices section is a dictionary with device names as keys. You could modify your script to get the first device's hostname, with something like this (Keep in mind that this solution assumes that the order of devices in the YAML file is consistent and that you always want to access the first device)
 
device_keys = list(self.parameters['testbed'].devices.keys())
first_device_name = device_keys[0]
self.parameters['uut'] = self.parameters['testbed'].devices[first_device_name]
hostname = self.parameters['uut'].hostname  # assuming there is a 'hostname' attribute in your device object

 Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io