Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Example: Using Chef for Junos OS to Configure Any Hierarchy Level

 

This example shows how you can use the netdev_group resource in the netdev cookbook to write recipes that configure any hierarchy level on devices running Chef for Junos OS. For more information about the light-weight resources in the netdev cookbook, see Chef for Junos OS at https://docs.chef.io/junos.html .

Requirements

This example uses the following hardware and software components:

  • A properly set up and configured Chef workstation and Chef server

  • Junos OS Release 16.1

  • A Juniper Networks device that the Chef client manages

    Note

    This example uses the netdev_group resource that is not supported on an OCX1100 switch. Only the netdev_interface resource is supported on an OCX1100 switch.

Before you begin, make sure that the local autonomous system number is already defined on the device.

Overview

The netdev_group resource specifies an Embedded Ruby (ERB) template file that defines a Junos OS configuration to be applied to the groups hierarchy level on the device. For information about Chef cookbook templates, see https://docs.chef.io/templates.html. When the client downloads the catalog, it adds the configuration data generated by the template under the [edit groups] hierarchy level and configures the apply-groups statement to include the group name. If the commit succeeds, the configuration inherits the statements in the configuration group. The configuration file is created in /var/tmp/name, where name is the name of a Junos OS group on the Chef client.

The netdev_group resource has the following actions:

  • :create–Create a Junos OS group (default).

  • :delete–Delete a Junos OS group.

The netdev_group resource has the following attributes:

  • name–The name of the Junos OS group under which configuration is applied.

  • template_path–The path of the template used to create the Junos OS configuration file in the format template-file-name.config-format.erb, where template-file-name is the name of the file and config-format is one of xml, set, or text. If config-format is not specified, xml is the default format.

  • variables–(Optional) Variables input to the template file.

Configuration

This example creates a ntedev_group resource named bgp_create.rb that configures statements for internal and external BGP peering. The netdev_group resource references the bgp.xml.erb template that generates the configuration data for the resource. The template is located in the netdev/templates/junos directory. The attributes that apply to the template are defined in netdev/attributes/default.rb under the variable name bgp.

The BGP variable definition contains the node-specific configuration values that the template uses to generate the configuration data for that group. The data is provided in a hash that uses the BGP group names as keys. Each key maps to another hash that contains the details for that group including the group type, and the IP addresses and AS number of the peers. When the template is referenced, it iterates over the hash and generates the Junos OS configuration data for the groups command.

Creating the netdev_group Resource

Step-by-Step Procedure

To create the netdev_group resource:

  1. From the chef-repo directory on the Chef workstation, download the netdev cookbook and extract the cookbook files to the cookbooks directory.

    knife cookbook site download netdev

    tar -zxvf netdev-n.n.n.tar.gz -C cookbooks

  2. Copy the netdev cookbook to create a new cookbook, netdev_device, in the cookbooks directory.
  3. In an editor of your choice, write the bgp_group recipe for creating the BGP configuration in the cookbooks/netdev_device/recipes/ bgp_create.rb file.

Creating the ERB Template

Step-by-Step Procedure

To create and stage the ERB template:

  • Create a new template file named bgp.xml.erb in the netdev/templates/junos directory, and add the text and Ruby tags required to generate the desired configuration data, in Junos OS XML format, for the BGP resource.

Creating the Attributes for the Template

Step-by-Step Procedure

To create and save the attributes for the template:

  • Add the BGP attributes to the end of the end of the default.rb file:

Configuring the Device by Using Chef for Junos OS

Step-by-Step Procedure

To configure the device by using Chef for Junos OS:

  1. Upload the netdev_device cookbook to the Chef server.

    $ knife cookbook upload netdev_device

  2. Edit the node object that represents the device.

    $ knife node edit device_node_name

    Knife starts your editor and opens a JSON file that contains the node attributes.

  3. Enter the recipe in the run-list attribute and then save the JSON file.

    The order in which you enter the recipes matters. The last configuration overrides any previous configuration.

  4. Log in as the root user.
  5. From the UNIX-level shell, run the Chef client.
    • If the Juniper Networks version of the Chef client is 2.x (for example, Chef client version 11.10.4_2.0), enter:

      %/opt/jet/chef/bin/ruby /opt/jet/chef/bin/chef-client -c /var/db/chef/client.rb

    • If the Juniper Networks version of the Chef client is 1.x (for example, Chef client version 11.10.4_1.1), enter:

      %/opt/sdk/chef/bin/ruby /opt/sdk/chef/bin/chef-client -c /var/db/chef/client.rb

    The Chef client displays status messages during its run to indicate its progress in performing the configuration.

Verification

To verify that the commit was successful and the configuration reflects the new BGP resource, perform these tasks:

Verifying the Commit

Purpose

Action

Meaning

The JUNOS: OK: COMMIT success! message and the commit log indicate that the Chef client successfully applied the configuration changes generated by the template.

Verifying the Configuration

Purpose

Verify that the BGP configuration group is in the active configuration on the device and that the configuration group name is configured for the apply-groups statement.

Action

From operational mode, enter the show configuration groups bgp_group and the show configuration apply-groups commands.

chef@chef-client> show configuration groups bgp_group
chef@chef-client> show configuration apply-groups

Meaning

The output shows that the BGP configuration was successfully configured in the groups hierarchy and that bgp_group was added to the apply-groups hierarchy.