Skip to content

Instantly share code, notes, and snippets.

@jeffbrl
Last active March 17, 2016 18:51
Show Gist options
  • Select an option

  • Save jeffbrl/9485d1e0052151d69450 to your computer and use it in GitHub Desktop.

Select an option

Save jeffbrl/9485d1e0052151d69450 to your computer and use it in GitHub Desktop.
Ansible - Using Juniper junos_cli module to execute CLI commands
# Playbook for executing CLI commands with output to text files
#
# Single host:
# ansible-playbook -i "router_name_here," -e '{ "CMD": "your_command_here" }' run-junos-cmds.yml
# Multiple hosts:
# ansible-playbook -i inventory_file -e '{ "CMD": "your_command_here" }' run-junos-cmds.yml
- name: Run CLI commands
hosts: all
connection: local
gather_facts: no
roles:
- Juniper.junos
vars:
temp_dir: /tmp
vars_prompt:
- name: USER
prompt: Device user
private: no
- name: PASSWORD
prompt: Device password
private: yes
tasks:
- name: create temporary filename
shell: mktemp "{{ temp_dir }}"/`date +"%Y%m%d%d%m"`"_{{ inventory_hostname }}".XXXXX
register: temp_file
- name: Running commands
junos_cli:
host={{ inventory_hostname }}
user={{ USER }}
port=22
passwd={{ PASSWORD }}
cli={{ CMD }}
logfile=cli.log
dest="{{ temp_file.stdout }}"
format=text
- name: print temp_file to stdout
command: cat "{{ temp_file.stdout }}"
register: foo
- debug: msg="{{ foo.stdout_lines }}"
@jeffbrl
Copy link
Copy Markdown
Author

jeffbrl commented Mar 17, 2016

This playbook writes the CLI output to files with a unique names. I wouldn't be surprised if there were an easier way to do this in ansible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment