juniper_junos_command

Execute one or more CLI commands on a Junos device

New in version 2.0.0.

Synopsis

  • Execute one or more CLI commands on a Junos device.
  • This module does NOT use the Junos CLI to execute the CLI command. Instead, it uses the <command> RPC over a NETCONF channel. The <command> RPC takes a CLI command as it’s input and is very similar to executing the command on the CLI, but you can NOT include any pipe modifies (i.e. | match, | count, etc.) with the CLI commands executed by this module.

Requirements

The following software packages must be installed on hosts that execute this module:

Module-specific Options

The following options may be specified for this module:

parameter type required default choices comments
commands
list yes none
A list of one or more CLI commands to execute on the Junos device.

aliases: cli, command, cmd, cmds
dest
path no None
The path to a file, on the Ansible control machine, where the output of the cli command will be saved.
The file must be writeable. If the file already exists, it is overwritten.
When tasks are executed against more than one target host, one process is forked for each target host. (Up to the maximum specified by the forks configuration. See forks for details.) This means that the value of this option must be unique per target host. This is usually accomplished by including {{ inventory_hostname }} in the value of the dest option. It is the user's responsibility to ensure this value is unique per target host.
For this reason, this option is deprecated. It is maintained for backwards compatibility. Use the dest_dir option in new playbooks. The dest and dest_dir options are mutually exclusive.

aliases: destination
dest_dir
path no None
The path to a directory, on the Ansible control machine, where the output of the cli command will be saved. The output will be logged to a file named {{ inventory_hostname }}_command.format in the directory specified by the value of the dest_dir option.
The destination file must be writeable. If the file already exists, it is overwritten. It is the users responsibility to ensure a unique dest_dir value is provided for each execution of this module within a playbook.
The dest_dir and dest options are mutually exclusive. The dest_dir option is recommended for all new playbooks.

aliases: destination_dir, destdir
formats
str or list of str no text
  • text
  • xml
  • json
The format of the reply for the CLI command(s) specified by the commands option. The specified format(s) must be supported by the target Junos device. The value of this option can either be a single format, or a list of formats. If a single format is specified, it applies to all command(s) specified by the commands option. If a list of formats are specified, there must be one value in the list for each command specified by the commands option. Specifying the value xml for the formats option is similar to appending | display xml to a CLI command, and specifying the value json for the formats option is similar to appending | display json to a CLI command.

aliases: format, display, output
return_output
bool no True
  • yes
  • no
Indicates if the output of the command should be returned in the module's response. You might want to set this option to false, and set the dest_dir option, if the command output is very large and you only need to save the output rather than using it's content in subsequent tasks/plays of your playbook.

Examples

---
- name: Examples of juniper_junos_command
  hosts: junos-all
  connection: local
  gather_facts: no
  roles:
    - Juniper.junos

  tasks:
    - name: Execute single "show version" command.
      juniper_junos_command:
        commands: "show version"
      register: response

    - name: Print the command output
      debug:
        var: response.stdout

    - name: Execute three commands.
      juniper_junos_command:
        commands:
          - "show version"
          - "show system uptime"
          - "show interface terse"
      register: response

    - name: Print the command output of each.
      debug:
        var: item.stdout
      with_items: "{{ response.results }}"

    - name: Two commands with XML output.
      juniper_junos_command:
        commands:
          - "show route"
          - "show lldp neighbors"
        format: xml

    - name: show route with XML output - show version with JSON output
      juniper_junos_command:
        commands:
          - "show route"
          - "show version"
        formats:
          - "xml"
          - "json"

    - name: save outputs in dest_dir
      juniper_junos_command:
        commands:
          - "show route"
          - "show version"
        dest_dir: "./output"

    - name: save output to dest
      juniper_junos_command:
        command: "show system uptime"
        dest: "/tmp/{{ inventory_hostname }}.uptime.output"

    - name: save output to dest
      juniper_junos_command:
        command:
          - "show route"
          - "show lldp neighbors"
        dest: "/tmp/{{ inventory_hostname }}.commands.output"

    - name: Multiple commands, save outputs, but don't return them
      juniper_junos_command:
        commands:
          - "show route"
          - "show version"
        formats:
          - "xml"
          - "json"
        dest_dir: "/tmp/outputs/"
        return_output: false

Return Values

name description returned type sample
changed
Indicates if the device's state has changed. Since this module does not change the operational or configuration state of the device, the value is always set to false.
You could use this module to execute a command which changes the operational state of the the device. For example, clear ospf neighbors. Beware, this module is unable to detect this situation, and will still return the value false for changed in this case.
success bool False
command
The CLI command which was executed.
always str
failed
Indicates if the task failed. See the results key for additional details.
always bool
format
The format of the command response.
always str
msg
A human-readable message indicating the result.
always str
parsed_output
The command reply from the Junos device parsed into a JSON data structure. For XML replies, the response is parsed into JSON using the jxmlease library. For JSON the response is parsed using the Python json library.
When Ansible converts the jxmlease or native Python data structure into JSON, it does not guarantee that the order of dictionary/object keys are maintained.
when command executed successfully, return_output is true, and the value of the formats option is xml or json. dict
results
The other keys are returned when a single command is specified for the commands option. When the value of the commands option is a list of commands, this key is returned instead. The value of this key is a list of dictionaries. Each element in the list corresponds to the commands in the commands option. The keys for each element in the list include all of the other keys listed. The failed key indicates if the individual command failed. In this case, there is also a top-level failed key. The top-level failed key will have a value of false if ANY of the commands ran successfully. In this case, check the value of the failed key for each element in the results list for the results of individual commands.
when the commands option is a list value. list of dict
stdout
The command reply from the Junos device as a single multi-line string.
when command executed successfully and return_output is true. str
stdout_lines
The command reply from the Junos device as a list of single-line strings.
when command executed successfully and return_output is true. list of str


Notes

Note

  • The NETCONF system service must be enabled on the target Junos device.

Author

  • Juniper Networks - Stacy Smith (@stacywsmith)

Status

This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.