Configure Devices

This topic describes how to push configuration to your network devices, by configuration CLIs directly, and by using the Conf module of the pyATS Library to quickly and easily.

Directly

The simplest way to push configuration to your network devices is to send raw configuration strings (as you would’ve typed it in device CLI) directly.

Once your device is connected (following Connect To Devices), you should be able to access a device’s configure() method:

device.configure('''
    interface Ethernet1/1
    no shutdown
''')

You can pass multi-line configurations to the configure() method, and the configuration would be applied accordingly.

Because this method accepts configuration as a raw string, you are responsible of generating the correct configuration syntax and context. This can be done using various programming techniques such as variable subsitution, and/or string templating (eg, Jinja2).

Using Genie Conf Objects

You take care of the what — the pyATS Library takes care of the how!

The pyATS Library Conf module provides a way for you to configure a network device without having to build the configuration yourself. Instead, you can generate reusable, multi-line configuration strings and apply them to one or more devices all at once.

Because the pyATS Library uses a common, feature-based structure across platforms, you can save time and effort when you automate your network testing.

Like the parser, the pyATS Library Conf module uses the same key-value pair structure across devices. This results in a consistent set of keys, which means that you can write one script that works to configure different devices.

You simply define the feature attributes, and the pyATS Library figures out how to apply the configuration to each different device.

To see a complete list of the structure and keys, visit the Models page, select a feature, and then select MODEL.

Examples

This topic describes how to use the Python interpreter or a Python script to use the Conf module functionality. Because you use it primarily for automated test scripts, we have not provided a command line option.

The process to configure devices is simple:

  1. Define the device (object) attributes.

  2. Tell the pyATS Library to apply the configuration.

Attention

Before you try these examples, make sure that you download and extract the zip file that contains the mock data and Python script.

Configure Feature on Device

This example shows you how to configure a single feature on a single device. You can use the device hostname or the device alias (defined in the testbed YAML file). In the following example, uut is the alias “unit under test” for the host nx-osv-1.

  1. In your virtual environment, change to the directory that contains the mock YAML file:

    (pyats) $ cd mock
    
  2. Load the testbed API and create your testbed and device objects:

    (pyats) $ pyats shell --testbed-file mock.yaml
    
        >>> uut = testbed.devices['uut']
    
  3. Get the pyATS Library Interface functionality, to configure an interface on the uut device:

    >>> from genie.conf.base import Interface
    
  4. Connect to the device and tell the system to create an NXOS interface:

    >>> uut.connect()
    
  5. Create an NXOS interface:

    >>> nxos_interface = Interface(device=uut, name='Ethernet4/3')
    
  6. Configure the interface that you just created:

    >>> nxos_interface.ipv4 = '200.1.1.2'
    >>> nxos_interface.ipv4.netmask ='255.255.255.0'
    >>> nxos_interface.switchport_enable = False
    >>> nxos_interface.shutdown = False
    
  7. Verify that the system generated the configuration:

    >>> print(nxos_interface.build_config(apply=False))
    

    The argument (apply=False) shows you what will be applied on the device if you go ahead with the build.

    Result: The system displays the following configuration information:

    interface Ethernet4/3
    no shutdown
    no switchport
    ip address 200.1.1.2 255.255.255.0
    exit
    
  8. To build the configuration and apply it to the device:

    >>> nxos_interface.build_config(apply=False)
    

    We’ve included the argument (apply=False) because you can’t actually build the configuration on a mock device.

  9. To remove the configuration from the device:

    >>> nxos_interface.build_unconfig(apply=False)
    

Change One Attribute

If you want to change the configuration of a device, or if you want to partially configure a device, you can tell the pyATS Library which attributes to apply.

By default, the pyATS Library applies the configuration from step 6 of the previous example. To limit the configuration to a single attribute, you can specify the attribute in an argument:

>>> nxos_interface.build_config(apply=False, attributes={'ipv4':None})

In this example, the system applies only the configuration of the ipv4 attribute to the device. Because the system uses a dictionary that stores key-value pairs, None serves as a placeholder value that has no effect on the configuration.

Configure Multiple Devices

You can apply configuration settings to all the devices in your testbed, rather than to a specific device or feature. This means that you can do all of the configuration, and then apply the settings with just one “build”.

  1. In your virtual environment, change to the directory that contains the mock YAML file:

    (pyats) $ cd mock
    
  2. Load the testbed API and create your testbed and device objects:

    (pyats) $ pyats shell --testbed-file mock.yaml
    
        >>> uut = testbed.devices['uut']
    
  3. Get the pyATS Library functionality that you need to create each feature, for example:

    >>> from genie.conf.base import Interface
    >>> from genie.libs.conf.ospf import Ospf
    >>> from genie.libs.conf.isis import Isis
    >>> from genie.libs.conf.rip import Rip
    
  4. Connect to the device and tell the system to create an NXOS interface:

    >>> uut.connect()
    
  5. Create two NXOS interfaces:

    >>> nxos_interface = Interface(device=uut, name='Ethernet4/3')
    >>> nxos_interface = Interface(device=uut, name='Ethernet4/4')
    
  6. Configure all of the features on all of your testbed devices, line by line.

    At this point, we provide examples because you cannot run the testbed.build_config command on the mock data. This example shows two devices, each with its own interface.

    Tip

    Refer to the feature model page to see which attributes (keys) you can configure.

    testbed.build_config()
     >>> [2018-09-25 09:55:39,982] +++ csr1000v-1: config +++
        config term
        Enter configuration commands, one per line.  End with CNTL/Z.
        csr1000v-1(config)#interface GigabitEthernet1
        csr1000v-1(config-if)# ip address 200.1.1.2 255.255.255.0
        csr1000v-1(config-if)# no shutdown
        csr1000v-1(config-if)# logging event link-status
        csr1000v-1(config-if)# ipv6 enable
        csr1000v-1(config-if)# exit
        csr1000v-1(config)#end
        csr1000v-1#
        [2018-09-25 09:55:41,382] +++ nx-osv-1: config +++
        config term
        Enter configuration commands, one per line.  End with CNTL/Z.
        nx-osv-1(config)# interface Ethernet4/7
        nx-osv-1(config-if)#  no shutdown
        nx-osv-1(config-if)#  logging event port link-status
        nx-osv-1(config-if)#  no switchport
        nx-osv-1(config-if)#  ip address 200.1.1.2 255.255.255.0
        nx-osv-1(config-if)#  exit
        nx-osv-1(config)# end
        nx-osv-1#
    

    Result: This builds and applies the configuration settings all at once to your testbed devices.

Note

Remember that the output of the configuration may vary depending on the device context (such as cli or YANG), but the configuration keys remain the same across all devices!

See also…