Ansible for network device management Part 4 using the Napalm finished library +gabriele method + loop

Source: Internet
Author: User

The last time with napalm implementation of a port plus description small function, but that is only used to give a device a port plus description, not implemented once to multiple devices, and did not implement a to multiple port plus (although this is not achieved, but fast).

Today only discusses iOS, the old iOS.

The function of the loop is implemented through bash, in fact, it can also be python, but my python is too slag, and did not achieve through ansible, because I am still not very likely to use the hosts, so with the bash. The ability to add configurations is implemented through napalm. In fact, there are two ways to add a "configuration file" (Config fragment configuration snippet) to iOS via napalm. The first one, as Gabriele's blog says, is the second one, as Kirk's blog says.

The first method I'm using now. However, I found that execution speed is very low, I can imagine, bash do loop, Python calls napalm function, napalm in the Netmiko to connect the real device. I'll write down the first method first, the next one, and then the second method, as follows:

The following is a python file, and the preparation before writing this Python file has a full description in the previous article.

__author__='Harry' fromNetmikoImportConnecthandler fromNapalmImportGet_network_driver fromSysImportArgvscript, hostname=Argvget_network_driver ('iOS') Driver= Get_network_driver ('iOS')Print "dealing with%s"%hostnameDevice= Driver (hostname,'Mark1','mark1 ' S_plaintext_password') Device.open ()device.load_merge_candidate (filename='/users/harry/pycharmprojects/bulktest/new_user_adding.conf') Device.compare_config ()
Output=Device.compare_config ()Printoutputdevice.commit_config () device.close ( )

In fact, the content is almost identical to the one in the Python >>> interface, but this time it is a host name from outside to this Python script, so execute it as follows.

In my Mac OS x shell, in this folder directory, execute Python bulktest.py machine-name.whatever.com, which will pass this hostname to Python The hostname variable goes up in script, a very simple Python syntax (learn Python's lesson in the hard one).

In order to execute this Python script multiple times, I put a loop in bash to execute this command multiple times, and this bash is as follows (why it's not fast enough to get up, it's weird)

#!/bin/bash more while read line;  Do               python bulktest.py $linedone

This core_switches is an ordinary file, which holds a bunch of hostname, each hostname occupy a row, that is, in the middle with a carriage return cut open, so more of it, it is easy to be read,read is the function of bash. The read is put into the while loop, and the function of the loop is done, and this bash executes as well.

I do not remember very clearly, it seems to need to give this bash permissions to change to 755, it can be in the./+tab time, that is, as an executable bash command, I have not tried sudo will not OK, may be feasible.

Ansible for network device management Part 4 using the Napalm finished library +gabriele method + loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.