Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


Example: Using Salt to Configure Devices Running Junos OS

Juniper Networks provides support for using Salt to manage devices running Junos OS. This example uses Salt to configure several devices running Junos OS.


This example uses the following hardware and software components:

  • Three MX Series routers running Junos OS with NETCONF enabled

  • Salt master with the following requirements:

    • Salt version 3001 or later

    • Can ping and perform operations on the devices running Junos OS


This example defines a Salt state that configures BGP peering sessions on the target devices running Junos OS. Table 1 outlines the device hostnames, proxy IDs, and device-specific files.

Table 1: Device Information


Proxy ID

Proxy Configuration File


BGP Data File














The example uses the following components:

  • Jinja2 configuration template—Defines the BGP configuration as a Jinja template using set command format. The template uses the .set file extension to indicate the format.

  • Pillar files—Define the device-specific configuration data required by the Jinja2 template. The data for each device is stored in a separate file under the /srv/pillar/bgp directory on the Salt master.

  • Pillar top file—Maps each pillar file to the appropriate proxy minion.

  • State file—Defines the state to apply to the target devices. In this case, the state file uses the junos.install_config function to apply the BGP configuration to a device.

  • State top file—Maps the state file to the devices on which the state should be applied.

In this example, you create a Jinja2 template to generate the BGP configuration that gets loaded and committed on the device. The template substitutes in variables for device-specific configuration data so that it can be reused as needed. The template file is placed under the /srv/salt/configs directory on the Salt master.

You then create separate pillar files for each device and define the device-specific configuration data in the file. Each pillar file defines a BGP_data key that contains all of the variable parameters that are referenced in the configuration template. The pillar top file maps each pillar file to its respective proxy minion. Storing the data in pillars ensures that each proxy minion can only access the data for that device.

The junos_bgp_config.sls state file defines a state that uses the junos.install_config function to apply the BGP configuration in the template. When invoked, the function locks the configuration database, loads the configuration specific to the target device, commits the configuration, and then unlocks the database. The function includes the diffs_file parameter to store the configuration differences in a file on the proxy minion server. The filename uses the id grain to generate device-specific filenames for the output files.

The state top file applies the junos_bgp_config state to any minion with a BGP_data pillar key with any value. When you run a highstate or apply that state to a device, Salt uses the template and pillar data to generate the configuration specific to the target and then invokes the junos.install_config function to load and commit the configuration on the device. If the configuration is already applied, the junos.install_config function does not reapply the configuration.

After you configure the devices, you then apply the junos_bgp_verify_peers state. This state executes the get-bgp-neighbor-information RPC on each device until it returns a peer-state value of Established for each peer defined in that device’s pillar data or times out. This state assumes that BGP is running on the device.


To configure the devices using Salt, perform the tasks included in this section.

Define the Pillar Data

Step-by-Step Procedure

To define the pillar data that is used with the Jinja2 template to generate the BGP configuration:

  1. On the Salt master, create a separate file named /srv/pillar/bgp/hostname.sls for each managed device.

  2. Define the data for host r1 in the r1.sls file.

  3. Define the data for host r2 in the r2.sls file.

  4. Define the data for host r3 in the r3.sls file.

  5. In the pillar top file, map the pillar file with the BGP data under the appropriate device ID to enable the device to access the data.

  6. Refresh the pillar data.

Define the Jinja2 Template

Step-by-Step Procedure

To create the Jinja2 template that is used to generate the BGP configuration:

  1. Create a file named /srv/salt/configs/junos-config-bgp-template.set on the Salt master.

  2. Add the BGP configuration template to the file and save it.

Define the States

Define the Configuration State File

To define the state file that applies the configuration:

  1. Create a file named /srv/salt/junos_bgp_config.sls on the Salt master.

  2. Define a state that uses the junos.install_config function to apply the BGP configuration in the template.


    If your template only includes Salt internal variables like pillar data, grain data, and functions, the junos.install_config function might need to define template_vars: True in order to render the template.

  3. In the top file, define the targets to which the junos_bgp_config state applies. The targets comprise all devices that have a BGP_data pillar item defined.

Define the BGP Verification State File

To define the state that verifies that the BPG peers have a peer-state of Established:

  1. Create a file named /srv/salt/junos_bgp_verify_peers.sls on the Salt master.

  2. Define a state that uses the junos.rpc function to execute the get-bgp-neighbor-information RPC and then checks the RPC reply for a peer-state of Established.


When you execute a highstate, the states in the top.sls file are applied to the appropriate target devices.


Verifying the BGP Configuration


Verify that the BGP session is established for each neighbor address.


Apply the junos_bgp_verify_peers state to the target devices and review the output.


The state file requires that the peer-state is equal to Established for each peer. The output indicates that this condition is met for all peers on each device. Alternatively, you can use the junos.cli function to execute the show bgp summary command on the devices and review the output to verify that the BGP session is established for each neighbor address.


Troubleshooting Configuration Load Errors


The Salt master generates a ConfigLoadError error indicating that it failed to load the configuration on the device because of a syntax error.


Salt renders the Junos OS configuration by using the Jinja2 template and the pillar data defined for that device. Salt generates a syntax error when the Jinja2 template produces an invalid configuration. To correct this error, update the Jinja2 template to correct the element identified by the bad_element key in the error message.