Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Configuring Salt Reactors for Junos OS Events

Salt reactors provide the ability to take action in response to specific events on the Salt event bus. The Salt Reactor system monitors the events on the Salt event bus and triggers actions for matching events based on the configured rules. You can enable the Junos syslog engine (for Salt) to publish events from managed devices running Junos OS to the Salt event bus. You can then configure existing or custom reactors to take action on those events. This enables you to create an event-driven infrastructure (EDI) to achieve closed-loop automation in real time.

How to Configure Salt Reactors

Before you can use Salt reactors to respond to Junos OS events, you must first enable the Junos syslog engine to publish Junos OS events to the Salt event bus and configure the managed devices running Junos OS to send syslog messages to the server on which the engine is running. For more information, see Junos Syslog Engine for Salt. After enabling the Junos syslog engine, you can configure Salt reactors that are automatically triggered in response to certain Junos OS events.

To configure a reactor that reacts to a specific event:

  1. Define the event and corresponding event tag that will trigger the action.

  2. Create one or more reactor SLS files on the Salt master and define the actions to take when the particular event occurs.

  3. In the Salt master configuration file, define the reactor option, and associate each event tag with one or more reactor files.

  4. Restart the Salt master to apply the updated configuration.

These steps are described in more detail in the following sections:

How to Define the Event and Event Tag

The Junos syslog engine publishes Junos OS events to the Salt event bus using the following default event tag string. The tag includes the target’s hostname and a Junos OS syslog event name.

To define the event tag to match on from the Salt event bus, specify the hostname and the event that will trigger the reaction. You can use an asterisk (*) to match on the event for all hosts. For detailed information about Junos OS system log messages, see the System Log Explorer.

For example, to match on a UI_COMMIT_COMPLETED event from any managed device running Junos OS, you can use the following event tag:

Note:

If you customize the event tag by configuring the topic parameter in the engines configuration, your matching event tag must reflect the same fields.

When you configure the reactor in the Salt master configuration file, you will reference this event tag.

How to Create the Reactor SLS File

Reactor SLS files define the actions to take when a specific event occurs. Reactor SLS files are similar to other Salt SLS files in that they use YAML format and support using Jinja. Generally, the files are stored in a separate directory, for example, /srv/reactor or /srv/salt/reactor. Reactor files can use data from the event, execute actions on the Salt master or minions, and apply states to a target, among other things. For detailed information about writing reactor files, see the official Salt documentation.

For example, the following reactor file applies the junos_backup_config state to the target device. In this case, the target is the proxy ID that matches the hostname value in the event data.

How to Configure the Reactor

To configure a reactor, define the reactor option in the Salt master configuration file. You define the event that triggers the action and the action to take. For each event that you want to match on and perform an action, associate the event tag for that event with one or more reactor SLS files. When the event is fired on the event bus, it automatically triggers the action defined in the file.

For example, the following reactor configuration calls the junos_backup_on_commit.sls reactor file whenever a UI_COMMIT_COMPLETED event occurs for any managed device running Junos OS that is configured to send syslog events to the Junos syslog engine:

To apply the reactor configuration, you must restart the Salt master using the commands appropriate for your operating system. For example:

Example: Responding to a Junos OS Commit Event

This example demonstrates a simple Salt reactor that generates a backup configuration file every time a managed device running Junos OS commits a configuration.

Requirements

This example uses the following hardware and software components:

  • Device running Junos OS with NETCONF enabled

  • Salt master with the following requirements:

    • Salt version 3001 (Sodium) or later

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

Overview

This example generates a backup configuration file every time a managed device running Junos OS commits a configuration. The example configures the Salt Reactor system to trigger the action when it matches on a UI_COMMIT_COMPLETED event. A sample event is shown here for reference:

In this example you first enable the Junos syslog engine on the Salt master and configure the device running Junos OS to send syslog messages to the Salt master server port 9999. After the configuration is applied, the device starts sending events to the Junos syslog engine, which publishes them on the Salt event bus.

You then create a reactor SLS file, junos_backup_on_commit, which applies the junos_backup_config state to the target identified by the value of the hostname key in the event data. The corresponding state file uses the junos.rpc function to execute the get_config RPC to retrieve the configuration in text format from the target device. It then saves the configuration on the proxy minion server at the file path specified by the dest parameter. The dest argument references the id grain and the current time to generate a unique filename that includes the device’s ID and a timestamp.

You then configure the reactor by defining the reactor option in the Salt master configuration file, which associates the event tag with the reactor file. After restarting the Salt master to apply the configuration, the Salt Reactor system invokes the appropriate reactor file when the event occurs. Thus, when a commit complete event occurs on the device running Junos OS that is managed by the Salt master, the Junos syslog engine publishes the event on the event bus, and the Salt Reactor system invokes the reactor file.

Configuration

Enable the Junos Syslog Engine

Step-by-Step Procedure

To enable the Junos syslog engine:

  1. Configure the Junos syslog engine in the Salt master configuration file.

  2. Restart the Salt master to apply the configuration, for example:

  3. Configure the device running Junos OS to forward syslog events to the same port on the Salt master.

Create the State file

Step-by-Step Procedure

To create the state file that generates a backup configuration file for the target node whenever the state is applied:

  1. On the Salt master, create a new state file named /srv/salt/junos_backup_config.sls.

  2. Define a state that retrieves the configuration and saves it to a uniquely named file.

    Note:

    If the proxy ID and hostname are not identical, you can reference the hostname by substituting {{ grains['junos_facts']['hostname'] }} for {{ grains['id'] }}.

Create the Reactor File

Step-by-Step Procedure

To create the reactor SLS file:

  1. On the Salt master, create a new reactor SLS file named /srv/reactor/junos_backup_on_commit.sls.

  2. Configure the reactor file to apply the junos_backup_config state to the target.

    • If the proxy ID is identical to the device hostname, you can reference the hostname in the event data as the target.

    • If the proxy ID is different from the device hostname, you can map the hostname to the proxy ID by referencing the value of the junos_facts:hostname grain.

Configure the Reactor

Step-by-Step Procedure

To configure the reactor:

  1. In the Salt master configuration file, configure the reactor option and specify the event and reactor file:

  2. Restart the Salt master to apply the configuration, for example:

Results

When you commit the configuration on the device running Junos OS, the Salt Reactor system matches on the event and calls the reactor. The reactor applies the state to the device, which retrieves and saves the configuration.

Verification

Verify the Salt Reactor Works

Purpose

Verify that the Salt reactor applies the junos_backup_config state and saves a backup configuration file to the proxy minion server when the device running Junos OS commits the configuration.

Action

After committing the configuration on the device running Junos OS, review the /var/log/salt/backup-configs directory on the proxy minion server to verify that the backup configuration file was generated.

Meaning

The configuration is saved in text format to the specified file on the proxy minion server.