Using Salt to Copy Files to Devices Running Junos OS
Salt can manage a device running Junos OS through a proxy minion
that establishes a NETCONF session over SSH with the device. When
you use a proxy minion, you can’t use standard Salt functions
to perform file copy operations on the device, because the device
does not directly run the salt-minion process. The salt.modules.junos.file_copy
execution function
and the salt.states.junos.file_copy
state function enable
you to copy files from the Salt master or proxy minion server to the
managed device running Junos OS.
junos.file_copy Function Syntax
The salt.modules.junos.file_copy
execution function
syntax is:
salt 'target' junos.file_copy src dest
The salt.states.junos.file_copy
state function syntax
is:
src: junos.file_copy: - dest: dest
where:
src
—Source file’s path. To specify a path on the Salt master, use salt:// notation. To specify a path on the proxy minion server, use an absolute path.dest
—Destination file’s absolute or relative path on the device running Junos OS. If you do not specify an absolute path on the target device, the path is relative to the current working directory, which is the user’s home directory.
How to Copy Files with the junos.file_copy Execution Function
The junos.file_copy
execution function enables you
to quickly copy a file from the Salt master or proxy minion server
to a device running Junos OS.
For example, the following command copies the bgp.slax file from the Salt master to the target device running Junos OS, router1, and renames the file to bgp-neighbors.slax:
saltuser@salt-master:~$ sudo salt 'router1' junos.file_copy salt://scripts/op/bgp.slax bgp-neighbors.slax router1: ---------- message: Successfully copied file from salt://scripts/op/bgp.slax to bgp-neighbors.slax out: True
Because the command does not specify an absolute path for the destination file, the file is copied to the home directory of the user.
saltuser@router1> file list ~ /var/home/saltuser: .ssh/ bgp-neighbors.slax
To copy the file to a specific location, specify the absolute path on the target device.
saltuser@salt-master:~$ sudo salt 'router1' junos.file_copy salt://scripts/op/bgp.slax /var/db/scripts/op/bgp-neighbors.slax router1: ---------- message: Successfully copied file from salt://scripts/op/bgp.slax to /var/db/scripts/op/bgp-neighbors.slax out: True
saltuser@router1> file list /var/db/scripts/op/ /var/db/scripts/op: bgp-neighbors.slax
How to Copy Files with the junos.file_copy State Function
You can use the junos.file_copy
state function within a Salt
state file to copy files from the Salt master or proxy minion server
to a device running Junos OS. This enables you to store a master set
of files in a single central repository and declare a state that defines
which files to copy to a given minion. When you enforce or apply the
state, Salt copies the necessary files from the repository to the
device.
The following state file copies two scripts from /srv/salt/scripts/op directory on the Salt master server to the /var/db/scripts/op directory on the device running Junos OS:
saltuser@salt-master:~$ cat /srv/salt/junos_copy_op_scripts.sls salt://scripts/op/bgp.slax: junos.file_copy: - dest: /var/db/scripts/op/bgp.slax salt://scripts/op/ospf.slax: junos.file_copy: - dest: /var/db/scripts/op/ospf.slax
When you apply the state to the target device, it copies each script to its specified location.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_copy_op_scripts router1: ---------- ID: salt://scripts/op/bgp.slax Function: junos.file_copy Result: True Comment: Started: 17:37:10.050920 Duration: 993.378 ms Changes: ---------- message: Successfully copied file from salt://scripts/op/bgp.slax to /var/db/scripts/op/bgp.slax out: True ---------- ID: salt://scripts/op/ospf.slax Function: junos.file_copy Result: True Comment: Started: 17:37:11.044443 Duration: 454.666 ms Changes: ---------- message: Successfully copied file from salt://scripts/op/ospf.slax to /var/db/scripts/op/ospf.slax out: True Summary for router1 ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 Total run time: 1.448 s
As another example, suppose you have a library of script files in the /srv/salt/scripts directory on the Salt master, and you want to ensure specific scripts are copied to the managed device running Junos OS.
saltuser@salt-master:/srv/salt/scripts$ ls -R .: commit op ./commit: bgp-config.slax ./op: bgp.slax ospf.slax
On the Salt master, you can specify the script information as pillar data and make that accessible to the appropriate minions in the pillar top file, for example:
saltuser@salt-master:~$ cat /srv/pillar/junos_scripts.sls junos_scripts: script_path: salt://scripts type: commit: - bgp-config.slax op: - bgp.slax - ospf.slax
saltuser@salt-master:~$ cat /srv/pillar/top.sls base: 'router1': - router1-proxy 'router2': - router2-proxy 'os_family:junos': - match: grain - junos_scripts
You can then create a Salt state file that uses a Jinja template to iterate over the script type and script name and generate the appropriate instructions to copy the scripts to the target devices.
saltuser@salt-master:~$ cat /srv/salt/junos_copy_scripts.sls {% for script_type, scripts in pillar['junos_scripts']['type'].items() %} {% for script in scripts %} {{ pillar['junos_scripts']['script_path'] }}/{{ script_type }}/{{ script }}: junos.file_copy: - dest: /var/db/scripts/{{ script_type }}/{{ script }} {% endfor %} {% endfor %}
Your Jinja syntax might vary depending on the version of Python that Salt uses.
The rendered state file becomes:
salt://scripts/commit/bgp-config.slax: junos.file_copy: - dest: /var/db/scripts/commit/bgp-config.slax salt://scripts/op/bgp.slax: junos.file_copy: - dest: /var/db/scripts/op/bgp.slax salt://scripts/op/ospf.slax: junos.file_copy: - dest: /var/db/scripts/op/ospf.slax
When you apply the state to the target device, it copies each script to the appropriate directory on the device for that script type.
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_copy_scripts router1: ---------- ID: salt://scripts/commit/bgp-config.slax Function: junos.file_copy Result: True Comment: Started: 17:23:19.049243 Duration: 1110.873 ms Changes: ---------- message: Successfully copied file from salt://scripts/commit/bgp-config.slax to /var/db/scripts/commit/bgp-config.slax out: True ---------- ID: salt://scripts/op/bgp.slax Function: junos.file_copy Result: True Comment: Started: 17:23:20.160245 Duration: 837.084 ms Changes: ---------- message: Successfully copied file from salt://scripts/op/bgp.slax to /var/db/scripts/op/bgp.slax out: True ---------- ID: salt://scripts/op/ospf.slax Function: junos.file_copy Result: True Comment: Started: 17:23:20.997516 Duration: 854.307 ms Changes: ---------- message: Successfully copied file from salt://scripts/op/ospf.slax to /var/db/scripts/op/ospf.slax out: True Summary for router1 ------------ Succeeded: 3 (changed=3) Failed: 0 ------------ Total states run: 3 Total run time: 2.802 s