Issuing Junos Commands Using Ansible raw Module

26 10 2016

If you want to issue something quick on a lot of devices, you don’t need to write a whole Ansible playbook to do that.  In fact you don’t really need the Junos module installed.

Ansible expects there to be Python on the managed device.  As you can read in this PacketPushers blog, it pushes the module out to the device and tries to execute it there.  Junos is going to get on-box Python at some point, but right now that’s roadmap (or SOPD if you must).

Suppose you want to find out what version of software you have on a your lab device, here’s a quick way to do that.

$ ansible 192.168.30.20 -m raw -a "show version" -u username -k
SSH password:
192.168.30.20 | SUCCESS | rc=0 >>
fpc0:
--------------------------------------------------------------------
Model: ex2200-24t-4g
JUNOS Base OS boot [12.3R12.4]
JUNOS Base OS Software Suite [12.3R12.4]
JUNOS Kernel Software Suite [12.3R12.4]
JUNOS Crypto Software Suite [12.3R12.4]
JUNOS Online Documentation [12.3R12.4]
JUNOS Enterprise Software Suite [12.3R12.4]
JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R12.4]
JUNOS Routing Software Suite [12.3R12.4]
JUNOS Web Management [12.3R12.4]
JUNOS FIPS mode utilities [12.3R12.4]

$

Perhaps you want to issue the same command across multiple devices?   Simply add them into the inventory file (by default, this is /etc/ansible/hosts) like so:

[Devices]
192.168.30.20
192.168.30.21
192.168.30.22
#192.168.30.23

As you can see above, individual hosts can be ‘remmed-out’ with a # at the start of the line.

Instead of specifying an IP address, you can now specify ‘all’ or just the group-name like this:

ansible all -m raw -a "show version" -u username -k
ansible Devices -m raw -a "show version" -u username -k

 

If you need to run several commands, you can daisy-chain them together using the semi-colon as a separator like so:

$ ansible Devices -m raw -a "show version;show system uptime" -u username -k
SSH password:
192.168.30.20 | SUCCESS | rc=0 >>
fpc0:
--------------------------------------------------------------------
Model: ex2200-24t-4g
JUNOS Base OS boot [12.3R12.4]
JUNOS Base OS Software Suite [12.3R12.4]
JUNOS Kernel Software Suite [12.3R12.4]
JUNOS Crypto Software Suite [12.3R12.4]
JUNOS Online Documentation [12.3R12.4]
JUNOS Enterprise Software Suite [12.3R12.4]
JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R12.4]
JUNOS Routing Software Suite [12.3R12.4]
JUNOS Web Management [12.3R12.4]
JUNOS FIPS mode utilities [12.3R12.4]
fpc0:
--------------------------------------------------------------------
Current time: 2016-01-26 01:33:51 UTC
System booted: 2016-01-25 21:48:18 UTC (03:45:33 ago)
Protocols started: 2016-01-25 21:51:32 UTC (03:42:19 ago)
Last configured: 2016-01-25 20:59:55 UTC (04:33:56 ago) by username
 1:33AM up 3:46, 1 user, load averages: 0.11, 0.11, 0.08

$

It is even possible to go into edit mode, apply some config and commit it:

$ ansible Devices -m raw -a "edit;set system host-name test;commit and-quit" -u username -k
SSH password:
192.168.30.20 | SUCCESS | rc=0 >>
Entering configuration mode
configuration check succeeds
commit complete
Exiting configuration mode

$

 

 

 

 

 

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: