Using Magic Variables for Service Discovery in Ansible

Gourav Shah
A free video tutorial from Gourav Shah
Premium Instructor| 35k+ students| Devops Trainer and Author
4.1 instructor rating • 14 courses • 41,347 students

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 gashing in this lesson we're going to start using those facts along with the magic variables and how we do our auto configuration followed balancers using the magic variable so fact gashing configuration is right there. And on the left hand side you see the facts being generated and we're going to use a code such similar to those to create our configuration to discover let's say ansible zero IPV for a dress 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 IPV for search for that and us zero and we find out that IPV for address for each zero. And that's what you see there so IPV for addresses that IP address. And that's what we want to fetch that IP address of that host that we want to fetch and add to other load balancer. Similarly we can find any other properties that you would want with the host device 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 role is a proxy the one we have taken from Galaxie So we'll go to the Galaxy the rules of proxy. You can also you know you customized You can also for this hour and then create Douro and roll and then customize it or for now we're going to just make changes to the proxied or to see if you to that that to template here. Let's examine this template and if you scroll down to the bottom of it and this is where the you know the configuration for back in servers are being done just you know full screen this and this is where it is being generated. So we have back in servers and is back in service at your proxy and back and so are the values that come from the variable that we define as part of our we have defined it as part of group first. You can do it in multiple places and if you go to the galaxies that your proxy rule and look at that variable description this is where it has been defined as a proxy and it's called back in and this goes over whatever you do find there. It gets added here. That's where the name and address for backing comes from. So this is like trading over stuff back in. Now we want to know what the list of hosts in our group called as apps over so we're going to use for host in group that's another variable that we could use as part of if you look at the magic variables example we can use that and we're going to do it over a group of forced from our inventory so that sales groups. And this is then went to group in that we have defined the observer's inventory. That's what is going to rate over and for host in-groups app. Now this back in name we change it with a magic variable and that's where we use data using coastwise. We've seen the example of force twice earlier but we start using it right here and we're going to get the host to ours for the whole tossed in that hydration for hosting groups. And then we defined which variable that would fetch from that host. And if it has been cached all year it will get fetch from there. So it's ansible hostname. That's what we all do out there. And similarly be copied and pasted here and we want to fetch. This time it's the IP IP address so it's going to be danceable underscore eat zero IPV for it's a multi level variable. So we use those brackets to define it. It's IPV for an address the same variable that we looked at earlier. That's the one on the bottom that's IPV for and address as danceable. Zero IPV 4 and the address. Let's add that here. So it's the host answerability IPV for address. We also need to define the board that it listens to. So port 80. That's very important Don't miss that port part. And again the hostname the same as the first of the first magic failure rates so. Host. Host ansible hostname we just copy and paste that same string here. Now this will allow us to iterate over our holes so whichever hosts are available at that point of time when you're running a playbook it will take that fetch. It's very both including host names and IP address and add one line each for that host in the configurations. And right now we have this then app one app to have three and let's run it and see what happens. So I'm running that ansible playbook site Dudamel 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 and Dale the file config file it entered at this time it was not able to find that attribute. Let's find out why and it's not able to find out that possibly because it was not run on that host. The fact that 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 balancing 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 observer's applications that was being added once it goes to the load balancers. You should see on the right hand side the trees were great and that's what you see now. So we've been able to use Baddeck variables to discover this information automatically. And if you are more sober 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.