ansible-junos-stdlib
ansible-junos-stdlib copied to clipboard
Add shell commands
Enhancement request:
Please provide a way to run BSD shell commands from Ansible playbooks.
Using PyEZ we can do this from the StartShell module:
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell
d = Device(host,user,passwd)
d.open()
with StartShell(d) as shell:
shell.run("echo /etc/passwd")
This can be done today using the <request-shell-execute>
RPC:
user@h0:~/$ cat pb.rpc_shell_example.yaml
---
- name: 'Execute a shell command'
hosts: junos-all
connection: local
gather_facts: no
roles:
- Juniper.junos
tasks:
- name: 'Get the /etc/passwd file'
juniper_junos_rpc:
rpc: 'request-shell-execute'
kwarg:
command: 'cat /etc/passwd'
register: response
- name: 'Print response'
debug:
var: response.parsed_output.output
Running the playbook produces:
user@h0:~/$ ansible-playbook -k --limit r0 ./pb.rpc_shell_example.yaml
SSH password:
PLAY [Execute a shell command] *****************************************************************************************
TASK [Get the /etc/passwd file] ****************************************************************************************
ok: [r0]
TASK [Print response] **************************************************************************************************
ok: [r0] => {
"response.parsed_output.output": "root:*:0:0:Charlie &:/root:/bin/csh daemon:*:1:1:Owner of many system processes:/root:/sbin/nologin operator:*:2:5:System &:/:/sbin/nologin tty:*:4:65533:Tty Sandbox:/:/sbin/nologin kmem:*:5:65533:KMem Sandbox:/:/sbin/nologin sshd:*:22:22:Secure Shell Daemon:/var/empty:/sbin/nologin ext:*:39:39:External applications:/:/sbin/nologin bind:*:53:53:Bind Sandbox:/:/sbin/nologin uucp:*:66:66:UUCP pseudo-user:/var/spool/uucppublic:/sbin/nologin nobody:*:65534:65534:Unprivileged user:/nonexistent:/sbin/nologin regress:*:928:20::/var/home/regress:/bin/csh remote:*:2000:20::/var/home/remote:/usr/sbin/cli user:*:2001:20::/var/home/user:/usr/sbin/cli user1:*:2002:20::/var/home/user1:/usr/sbin/cli user2:*:2003:20::/var/home/user2:/usr/sbin/cli"
}
PLAY RECAP *************************************************************************************************************
r0 : ok=2 changed=0 unreachable=0 failed=0
True, but request-shell-execute is not supported on all devices and Junos versions.