ansible.netcommon.cli_command (3.1.3) — module

Run a cli command on cli-based network devices

| "added in version" 1.0.0 of ansible.netcommon"

Authors: Nathaniel Case (@Qalthos)

This plugin has a corresponding action plugin.

Install collection

Install with ansible-galaxy collection install ansible.netcommon:==3.1.3


Add to requirements.yml

  collections:
    - name: ansible.netcommon
      version: 3.1.3

Description

Sends a command to a network device and returns the result read from the device.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: run show version on remote devices
  ansible.netcommon.cli_command:
    command: show version
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: run command with json formatted output
  ansible.netcommon.cli_command:
    command: show version | json
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: run command expecting user confirmation
  ansible.netcommon.cli_command:
    command: commit replace
    prompt: This commit will replace or remove the entire running configuration
    answer: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: run command expecting user confirmation
  ansible.netcommon.cli_command:
    command: show interface summary
    prompt: Press any key to continue
    answer: y
    newline: false
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: run config mode command and handle prompt/answer
  ansible.netcommon.cli_command:
    command: '{{ item }}'
    prompt:
    - Exit with uncommitted changes
    answer: y
  loop:
  - configure
  - set system syslog file test any any
  - exit
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: multiple prompt, multiple answer (mandatory check for all prompts)
  ansible.netcommon.cli_command:
    command: copy sftp sftp://user@host//user/test.img
    check_all: true
    prompt:
    - Confirm download operation
    - Password
    - Do you want to change that to the standby image
    answer:
    - y
    - <password>
    - y

Inputs

    
answer:
    description:
    - The answer to reply with if I(prompt) is matched. The value can be a single answer
      or a list of answer for multiple prompts. In case the command execution results
      in multiple prompts the sequence of the prompt and excepted answer should be in
      same order.
    elements: str
    required: false
    type: list

prompt:
    description:
    - A single regex pattern or a sequence of patterns to evaluate the expected prompt
      from I(command).
    elements: str
    required: false
    type: list

command:
    description:
    - The command to send to the remote network device.  The resulting output from the
      command is returned, unless I(sendonly) is set.
    required: true
    type: str

newline:
    default: true
    description:
    - The boolean value, that when set to false will send I(answer) to the device without
      a trailing newline.
    required: false
    type: bool

sendonly:
    default: false
    description:
    - The boolean value, that when set to true will send I(command) to the device but
      not wait for a result.
    required: false
    type: bool

check_all:
    default: false
    description:
    - By default if any one of the prompts mentioned in C(prompt) option is matched it
      won't check for other prompts. This boolean flag, that when set to I(True) will
      check for all the prompts mentioned in C(prompt) option in the given order. If the
      option is set to I(True) all the prompts should be received from remote host if
      not it will result in timeout.
    type: bool

Outputs

json:
  description: A dictionary representing a JSON-formatted response
  returned: when the device response is valid JSON
  sample: "{\n  \"architecture\": \"i386\",\n  \"bootupTimestamp\": 1532649700.56,\n\
    \  \"modelName\": \"vEOS\",\n  \"version\": \"4.15.9M\"\n  [...]\n}\n"
  type: dict
stdout:
  description: The response from the command
  returned: when sendonly is false
  sample: 'Version:      VyOS 1.1.7[...]'
  type: str