Using Netbox Dynamic Inventory in Ansible

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone in this video I want to go over using that box as a dynamic inventory source for ansible in this video i'm not going to do anything too crazy with the ansible it's gonna be a super simple playbook I really want to just go into using netbox for the dynamic inventory source so let me go to the terminal and and an ansible folder and in the ansible folder we have our typical ansible config I have my playbook which is create configs and then a net box inventory file so let's first take a look at the ansible config file pretty simple but as you can see I'm using the net box in the net box inv diamo file as my inventory now let's take a look at that file so this is obviously gonna look a really different you know compared to using a static inventory instead of listing all my hosts and listing the variables about the hosts and having the host VARs file and all that kind of stuff basically what we do is we have a yamo file using the net box plugin that calls the api end endpoint of localhost 8,000 which book Lajos 8000 is my net box so from there we put my API key and then we just have a couple of things validate certs fals config context false I don't have any config context in that box and this actually comes from if you go into net box go to your device's I'll click on the device config contacts I have nothing in here so config context is false and then group I I'm gonna group them by device roles this is because I want to differentiate between my routers and my switches so what is this file actually do well we can run a command and it's the ansible inventory command and basically we're gonna do is we're gonna list all the inventory from the net box inv IMO file so we click enter and let me scroll up for you okay so basically you can see what's going on here is going to the API we're grabbing the inventory we have host bars and we have a device with host bars for each individual device so you can see this is an ansible host r1 there's the name of the host which is the IP address we have device roles we have device types manufacturers whatever and when I grouped it by device roles if we scroll down to the bottom of this output we could see we have device roles routers here are my 10 routers device world switches here are my 4 switches ok great it gives us this output this doesn't really help us with the ansible file but it's a nice little visualization to show that our inventory file is working properly so I'm going to do LS again and this time I'm going to show you what's in my playbook so my screen kind of flaked there first some reason but anyway we have you know what I'll do actually let's just go into V and take a look at it because okay here's the first here's the name of my playbook crate config using that box we're doing local connection so I'm not actually going out to the devices and what I'm gonna do is run this play book on hosts in the device roles router group remember I had them list by device role so we could see the routers and switches I'm only gonna run this playbook against the routers we don't need to gather facts on the play books and I only have one task in this playbook generate config is using data obtained from netbox basically I have in this templates folder config template j2 and then the destination is gonna be output and we're gonna do the inventory host name dot config so let's exit out of here and what I'm gonna first do is you know let's take a look at templates and I have that config template file we'll just take a quick look at what that is actually doing pretty it's pretty simple I'm not gonna generate a full config I'm just gonna give it a hostname so we can see that it goes through all of our routers it figures one for each device and you know gives an IP address just a couple basic things again I didn't want this video to be about you know ansible in depth I just wanted to show you about how to use in that box for an inventory source so I'm actually gonna I already have an output folder and I'm just gonna remove that folder just so we know nothing is in it so let's do output sorry I have to make dere output so now we can LS there's nothing in it okay great so now let's run my ansible playbook if I could spell ansible correctly ansible playbook and his create configs so I'm going to run the playbook and we can see the played recap actually went through our one through our 10 it was okay and it changed for each specific one so now if we LS output we could see we have a config file for each of our 10 routers let's just go ahead and cat output our one dot can config well config and we could see hostname r1 there's our interface the IP address we connected to let's do this again with our two just to confirm that our playbook actually worked correctly there we go hostname r2 brick that p address etc so again this is a very simple playbook just using a ginger to template to create a configuration file but the thing that we really want to you know the net box inventory diamo is this is really the bread and butter of it all we have the net box plug-in which is an ansible plugin you don't need to install anything special you call your api with the token and you're done so this is a pretty simple task if you have any questions leave comments below and we'll see you on the next one
Info
Channel: Michael O'Brien's CCIE Journey
Views: 4,854
Rating: 5 out of 5
Keywords: Ansible, Netbox, Dynamic Inventory, Network Automation
Id: g6U0n4FHjSg
Channel Id: undefined
Length: 8min 3sec (483 seconds)
Published: Sun Mar 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.