Ansible For Network Automation Lesson 9: Working With APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi we're back again i'm josh vandery on ansible for network automation and today we're talking a little bit more about working with apis and we're going to also take a look at an open source tool called netbox at the end of the session we're going to have worked with some rest apis using ansible and then we're going to have looked at an open source tool in that box that's one of my favorites for automating as a source of truth first what is a rest api one of the things i like to say i know this isn't exactly 100 accurate but rest apis i put them in a similar boat as http request it is client server based it is stateless as well so that's a big important piece to this rest methods they like to call it crud which is create read update and delete some actions for these methods that correspond to the crud model is get being a read a post sending out and that is a create a put or a patch those are two different things in this model and they represent updating information and then last and foremost delete that is removing an item all right let's go ahead and take a look we'll take a look at netbox and ansible rest api usage so what is netbox netbox is a tool i've been using quite a bit it's an open source data center infrastructure management and ip address management tool so it's managing my physical devices it's managing my ip addresses and my prefixes in the network environment you can get more information about it at the url in the browser here at netbox.readthedocs.io and their github page is netbox community slash netbox so if you use your favorite search engine and search for netbox space github it'll take you to this page it is a very active open source project and one of the things that i like most about it is it has an api that is very friendly and well documented so first off this is netbox when we go over things we have our sites regions our tenants in a multi-tenant environment we can track our racks and elevations our devices we get various device roles manufacturers it really represents things pretty well in our ip addresses well it's ip addresses it also then with ip addresses come to your prefixes and then as you continue to grow up into your aggregates and then rrr so aggregates being address allocations from the registries and then it also tracks vrfs and vlans that's kind of a nifty piece to it and then also we'll get into doing virtual machines etc but this isn't about that and circuit so we'll go through that but one of the big things about working with apis is you need to know about information about it often well at least on netbox there's an api link on the bottom which takes you to a local representation of the api itself if you aren't seeing a obvious place where there's api use your favorite search engine and search product name and api so first thing i do often is i go ahead they've separated out applications based on circuits data center etc so as we're going to work through this we're going to try to figure out how to add an ip address to an interface ipam covers our ip addresses so we'll take a look there first as we scroll down this is everything that we can do with the api and so we're going to eventually work on updating an address and posting an address so we'll be able to take a look when we click on post and then try it out we'll get what is the information that's necessary to pass in and one of the things that we're going to find that we need to pass in if we're going to assign an ip address to an interface in netbox is we need to know what interface it is so to do that we'll eventually come back here to data center infrastructure management and that is where the actual interfaces are so we'll be able to get our interface id so that we know which interface it is in particular that we're looking at but we'll take a look more as we take a look at ansible itself let's take a look at our file structure that we're going to take a look at today we've got most of the same file structure that we've had before really we're going to focus on our demo api playbooks as we are just going to demonstrate how it is to work with apis nothing specifically about netbox or anything else in particular so one of the first things that we're going to do is take a look at this demo api file so in there we've got our first play we're going to be working with apis we've got our connection set to local and we're using hosts of localhost because we are working with apis it's important that we actually initiate things from our actual localhost because that's the machine that is actually going out and making the api calls we don't have any network devices actually going out to the api and making calls themselves that's the one little trick as far as working with apis and ansible this new key that we see on here vars underscore files i found a piece to move the secret information into the vars files so this is another key it will load the information up right away and then this way i can use this in the var section because i want to make the net box headers that i use for the api calls be as seamless as possible so in the vars we've got our net box underscore headers where we've got our content type and the authorization note the token there is a keyword that is required it is in the api doc so when you're working on the apis take a look at that and that's not something specifically to working with ansible with apis it's something api specific so our first task all we're going to do is get the list of all the devices we've got our uri module that is going to be used then we get the url is one of the parameters that we need to set so we have an http to netbox.local and then slash api slash dcim and devices that is something you get from the documentation we're using the method of get the status code we expect is 200 sometimes you might expect a 201 for a patch or a put and maybe a post as well you have to know what the documentation states back if you set this status code to 201 and you get a 200 status back you're gonna end up having the play fail because it was an unexpected status code if you are okay with a 200 a 201 and various other status codes you can put those in there with a comma and just have multiples listed 200 is the default if i remember right and that is in the ansible docs then we've got our headers and then we're going to save that to device results and then we're going to take a look at those so let's go ahead and take a look let's take a look at that playbook run here first and then we'll take a look at the actual the token that was added to the vault secrets here because when we do ask vault pass we're now also getting a net box token and that's why we're doing the ask vault password here we're getting the net box devices and that was incredibly quick so as we go through we can then scroll back up and let's take a look at the keys first and foremost there are just a few devices and this is a small net box environment this is representing the lab environment that we've had so on the return results we get our allow which says we can do a get post heading options for various things we have our api version is 2.6 etc change connection content length keep on going now we take a look at this key json is one of the important ones that's the return information we get our information we had six devices returned for our account we have our next if there was an api paging what that next url would be previous as well and then the results and so when we take a look at the results the first thing i take a look at is the fact that this is a list of results so we have a list it's defined by the square bracket and then we have another set of keys and another keys values being displayed so that's the information about this first device we've got it it's the border router the actual information lots of more information and so that's where apis come in handy is that you get a lot of information really quick and it's structured so you can dig down into it and we said we're going to take a look at the ansible vault and it's file secret info so we'll just take a look to show there is a net box token there that is something that you can look at how to create tokens within the net box documentation but that is what was added in for this lab environment and note that token is unique so after all this recording is done that will get changed on the local environment even though it is just a lab local environment we're going to jump ahead to the last api demo file here that we've got to take a look the the final picture what this is going to look like one thing i'm going to encourage you to do netbox is a free tool there are docker instances and also installation available so feel free to install netbox that on your own lab environment and do some testing with it put some devices into it and start to take a look at what things will look like and play around with the apis so this is going to be a single play we've got our virus files our vars up on top one thing i have done in this which is very helpful to be able to pull out is have the nut box underscore url so that way you can go ahead and change that if you have a lab environment start running things against lab and also as you develop your ansible playbooks you can use that of here's your stage inventory and have that as a variable is what is that netbox url there so as we go down our first task we're going to go ahead and get information from loopback7 on router 1. we're going to say let's get the interface what is that interface id for router 1 in netbox we're going to check to see if the address already exists this particular one is 10.20.30.7 so that's going to be uh netbox it's asking hey do i need to update this and then we're going to save that to the variable netbox ipcheck continuing down task number two we're going to assign this ip address in within netbox to the url of ip addresses our method is going to be a post so that's going to be writing and creating new the status code that we're expecting back on this is a 201 i got that from the docs headers we're using the netbox headers the body format is json json it's a very handy way of representing data and then our body we've got our address is going to be 10.20.30 and we do within that box need the slash 32 our status is one for active and then interface so here we're looking back at the first task we've got our registration netbox loopback interface then we go down into the json key we get the results key from the value going back to the value of from json and then we want the zero response back so we want the actual id from the response there could be multiple generally there should only be one response so the zero is a zeroth item there and then we want the id number so that's going to be the actual interface and then we're going to post that out to netbox and then that's going to be whenever the netbox ipcheck.results is less than one for the length that just says that hey there is no ip address already assigned to that interface and then we're going to print out the results at the end okay first let's go to netbox and take a look at router 1 to see what exists already for it so we'll go into our router 1 here we've got our interfaces that are assigned that were created so there's a gigabit ethernet 00123 and then there's two loopbacks that were created in netbox note that there are no ip addresses here whatsoever and we'll look to go ahead and add those so when we do our ansible playbook demo api 5 and make sure we ask for our vault pass now we'll go ahead and go create that address on netbox it's gone through it went pretty quick for especially for being in my lab environment and then when we take a look at refreshing this page we now have the loopback address is assigned so we're going to just take a look back at all the plays we see that we've got everything is checking out okay and then our post results we get our json information we've got our address when it was created and everything else that goes along with that so let's just review real quick what we've accomplished today we've looked at an open source tool netbox one of my favorite tools to automate with and we've worked with the rest apis specifically posts and gets now the one thing i will say on this one is these are all tools that are open source and freely available so i encourage you to go ahead and go out go to netbox that read the docs there's installation methods on how to do that ubuntu is the flavor that is most heavily documented although there is documentation for centos as well but i still if you have the option look at the ubuntu flavor of linux install that on a virtual machine somewhere in a lab and go ahead and get started enter some data into netbox and look at the apis and use ansible to do so they are all open source [Music] you
Info
Channel: Packet Pushers
Views: 463
Rating: undefined out of 5
Keywords: Ansible, network automation, networking, Packet Pushers, Josh Vanderaa, Netbox, source of truth, APIs, REST
Id: 0kY6UQlHOZ8
Channel Id: undefined
Length: 13min 27sec (807 seconds)
Published: Thu Jul 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.