napalm-automation / napalm-base

Apache License 2.0
32 stars 48 forks source link

Salt Grains for matching port descriptions and configuration on top of it #260

Closed icmp-echo closed 7 years ago

icmp-echo commented 7 years ago

Creating a new issue to follow up with issue #197

salt -G 'os:junos and interface:fe-0/0/5' grains.get description

icmp-echo commented 7 years ago

response from @mirceaulinic

salt -G 'os:junos and interface:fe-0/0/5' selects those devices running Junos and having the interface fe-0/0/5. The grains are used to select minions, not a particular feature / subset / subsystem.

I see what you are trying to achieve @icmp-echo. Searching for a particular detail indeed is very useful in production networks especially when it grows that much that you can't keep in mind all the details. For this, you may find this Salt runner useful: https://docs.saltstack.com/en/develop/ref/runners/all/salt.runners.net.html -- will be added in the next release, Nitrogen (later this month). To see how this runner works, see this video: https://www.youtube.com/watch?v=AqBk5fM7qZ0. In the first example it returns all interfaces having the pattern "cogent" in their description. Similarly you can search for mac addresses, ip addresses, interfaces, etc. In your case would probably be salt-run net.find fe-0/0/5. And it returns an output like:

Details for interface xe-0/0/0
_________________________________________________________________________________________________________________
|    Device    | Interface | Interface Description |  UP  | Enabled | Speed [Mbps] | MAC Address | IP Addresses |
_________________________________________________________________________________________________________________
| edge01.bjm01 | vt-0/0/10 |                       | True |   True  |     1000     |             |              |
_________________________________________________________________________________________________________________
| edge01.flw01 | vt-0/0/10 |                       | True |   True  |     1000     |             |              |
_________________________________________________________________________________________________________________
| edge01.pos01 | vt-0/0/10 |                       | True |   True  |     1000     |             |              |
_________________________________________________________________________________________________________________
| edge01.oua01 | vt-0/0/10 |                       | True |   True  |     1000     |             |              |
_________________________________________________________________________________________________________________

As an aside, check this out too: https://docs.saltstack.com/en/develop/ref/runners/all/salt.runners.bgp.html.

You can also use the grains to achieve this, but I would not recommend. Salt impresses with its speed and caching the interfaces under grains would slow down a bit the matching if you have a device with many interfaces. Grains should have a very simple structure.

But if you really want, you can, by writing a custom grains modules in your envirnment: https://docs.saltstack.com/en/develop/topics/grains/index.html#writing-grains See below an example:

def interfaces_grain(proxy):
    if proxy['napalm.initialized']:
        intrf = proxy['napalm.call']('get_interfaces')
        if intrf:
            return {'interfaces_details': intrf.get('out', {})}

And then you can select your devices like: salt -G 'os:junos and interfaces_details:fe-0/0/5:description:*pattern*

icmp-echo commented 7 years ago

@mirceaulinic Hey sir, when salt nitrogen will get released? and how to install net.find runner ?

mirceaulinic commented 7 years ago

@icmp-echo I don't know exactly when Salt Nitrogen will be released - it has been tagged as 2017.7 so it is previewed for July, but it can get delayed if the core team decides it is not stable or not safe to release.

The net.find runner will available in the Nitrogen package; you don't need to install it, but only to activate it (read the docs: https://docs.saltstack.com/en/develop/ref/runners/all/salt.runners.net.html#module-salt.runners.net -- hint: configure the mines properly).

Till the official release, you can take it from: https://github.com/napalm-automation/napalm-salt/blob/master/nitrogen/_runners/net.py (copy from there, paste it in a directory called _runners, under your extension modules directory which is either one of the file_roots paths, .e.g. /etc/salt/extmods/_runners where /etc/salt/extmods is defined under file_roots; or reference it directly using https://docs.saltstack.com/en/latest/ref/configuration/master.html#runner-dirs).

I believe this is not the right place for Salt-related questions so I am going to close this issue & feel free to reopen under https://github.com/napalm-automation/napalm-salt.