Using Magic Variables for Service Discovery in Ansible

A free video tutorial from Gourav Shah
Premium Instructor| 65k+ students| Devops Trainer and Author
Rating: 4.2 out of 5Instructor rating
15 courses
67,667 students
Using Magic Variables for Service Discovery

Learn more from the full course

Ultimate Ansible Bootcamp by School of Devops®

Unofficial Guide to EX407 Redhat Certified Specialist in Ansible Automation

07:32:45 of on-demand video • Updated July 2020

What is ansible, why its needed and how it works
How to configure ansible and setup static/dynamic inventories
Ad hoc server management, running jobs in parallel
Managing systems entities using ansible Modules
Writing Playbooks to define the state of infrastructure as a code
Creating modular, reusable code with Roles
Creating dynmic code with vars and templates
Using Ansible Galaxy
Encrypting sensitive data with Vault
Service Discovery and managing Multiple Environments
Application Deployment Strategies, Rolling Updates
English [Auto]
In the last lesson, we enabled the fact cashing in this lesson, we're going to start using those facts along with the magic variables and we going to add do our auto configuration for load balancers using the magic variable. So fact caching configuration is right there. And on the left hand side, you see the facts being generated. And we're going to use a code similar to this to create our configuration to discover, let's say, Ansible zero IP before address. And that comes from the fact. So you can actually go and look at, let's say, one of the files. And if you go and look at Ansible IP v4, search for that and it's zero. And we find out that IP v4 address for e0 and that's what you see there. So IPV four addresses that IP address and that's what we want to fetch that IP address of that horse that we want to fetch and add to our load balancer. Similarly, you can find any other properties that you would want with the host virus itself, the one we looked at in the magic variables. Now, where do we add? This is inside the template. And for us, the, you know, the rule as a proxy, the one we have taken from Galaxy. So we'll go to the galaxy, the rules at your proxy. You can also, you know, customarily customize. You can also fill this out and then create your own role and then customize it. Or for now, we're going to just make changes to the proxy that safety data that the genetic template here. Let's examine this template. And if you go scroll down to the bottom of it, and this is where the you know, the configuration for backend servers are being done, I'll just, you know, fullscreen this and this is where it is being generated. So we have backend servers and these back in servers at your proxy underscore back and server. The values for that come from the variable that we define as part of our we have defined it as part of group. You can do it from multiple places. And if you go to the galaxies that are proxy role and look at that variable description, this is where it has been defined. Let's say it's a proxy underscore back in undiscussable, whatever you define there, it gets added here. That's where the name and address for Bakin comes from. So this is like treating over a list of Parkin's. Now we want to put it over the list of host in our group called As Observer. So we're going to use Formost in group. That's another variable that we could use as part of our if you look at the magic variables example, we can use that and we're going to iterate over a group of hosts from our inventory. So that says groups. And this is the inventory group in that we have defined. That's the observer's inventory. That's what it's going to iterate over. And for most in groups app, now this back a name to change it with a magic variable, and that's where you start using host virus. We've seen the example of those twice earlier, but we start using it right here and we're going to get the host virus for the host host in that iteration, for host in groups app. And then we define which variable that will fetch from that host. And if it has been cached only earlier, it will get fetch from there. So it's ansible hostname. That's what we want to add there. And similarly, we copied and pasted here and we want to fetch this time it's the IP IP address, so it's going to be the ansible. Underscore it zero IPV four. It's a multi level variable. So we use those brackets to define its its IPV for and address. The same variable that we looked at earlier. That's the one on the bottom, that's IPV four and address. So it's to build it zero, IPV four and the address, let's add that here, so it's the host tangibility right before address. We also need to define the board that it listens to. So that's very important. Don't miss that board part. And again, the hostname. The same as the first holding, the first magic variable rate, so host versus host Ansible, whose name we just copy and paste that same string here. Now, this will allow us to iterate over our hosts or whichever hosts are available at that point of time. When you're running a playbook, it will ideato what that fetch its variables, including host names and IP address and add one line each for that host in the configurations. And right now we have this in the inventory up one up to have three. And let's run it and see what happens. So I'm running that ansible playbook, Saitoti a.m. this time. And I'm going to connect to the load balancer on the right hand side and watch the configurations, a watch minus and one until the final config file it edited out. This time, it was not able to find that attribute. Let's find out why. And it's not able to find out attribute possibly because it was not run on that hostilely. The facts were not cached. So you'll have to sequence it in a right way so that the application servers are executed first in the playbook, so that the facts are available when it actually goes to the load balancer. It has to be available now if you run this again. That should go through. All right, and once it goes through, you should see the observers applications that was being added, once it goes to the load balancers play, you should see on the right hand side the three servers are in and that's what you see now. So we've been able to use magic variables to discover this information automatically. And if you are to remove a server, it will also reflect here, you know, and this is a flexible way of doing these configuration. And that's how you use magic variables for service discovery.