Last active
March 17, 2016 18:51
-
-
Save jeffbrl/9485d1e0052151d69450 to your computer and use it in GitHub Desktop.
Ansible - Using Juniper junos_cli module to execute CLI commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 }}" | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.