Utilizing Roles in your Main Playbooks
Sprawling infrastructure and snowflake woes? Then Ansible is the solution you need!
Ansible is designed to be minimal in nature, consistent, secure and highly reliable! Ansible is a highly sought after skill in the marketplace with an extremely low learning curve for administrators, developers and IT managers.
The "Ansible Essentials: Simplicity in Automation Technical Overview" course introduces you to Ansible automation and configuration management, provisioning, deploying, and managing compute infrastructure across cloud, virtual, and physical environments.
By taking this course you'll learn just how easy it is to use Ansible to build consistent and repeatable infrastructure environments using Ansible playbooks.
In this video I am now going to show you how to call the roles that we have created in your main playbook. So I am in the site.yml file that we created during the basic playbook lesson before, and I have completely erased it because I want to start fresh. Now over here under the roles directory you can see that I have actually added a couple additional roles. So I have added a haproxy role and a mariadb role completely.
So now we basically have a full lamp stack with a haproxy load balancer, and really quickly I just kind of want to expand these to show you what I did. So, if I go into the main.yml file for the mariadb role, basically it’s similar to what we have already done with the other apache role. We are installing some packages, I got the mariadb package here and a couple of related packages that I need.
I am configuring SELinux, I am creating the configuration file, creating the log file, creating the PID directory, starting the service, starting firewalld. And then I am getting into, you know, inserting the rule and creating the app db and app dbuser. So that’s really all that I needed for the database component and just like what I did with the other roles I added in a templates file over here and I handled the handler in the handler’s directory.
The same thing for the haproxy role here. Basically downloading and installing haproxy, configuring the configuration file and starting the service, and I do have a handler as well as a template file that I did put inside the templates and handlers directory respectively. So now that I have all of those pieces in order to deploy the full stack, I can now call all of these roles from the main playbook. And so I go back to my site.yml here and I am going to start off declaring the roles in the order in which I want.
So this is going to be an example of a playbook with multiple plays because I am targeting different hosts. So I start off, apply the common configuration to all hosts, and I will do hosts: all there as my declaration, I am connecting as the vagrant user and becoming the root user in all of these, so that will be common, and then I am actually going to call my roles, so I am going to do common.
Great, so that’s play number one. Play number two, I am now going to apply the database configuration. And for that I am going to run it against the dbservers hosts, so then do remote_user: vagrant, become: yes and then I will get my role call here which is mariadb. Then my third role declaration, so here’s my third play, I am going to apply the apache and the web roles. Targeting the webservers group, do remote_user: vagrant, become: yes and then I am going to call two roles here.
So I am going to start with the apache and then end with web and then to finish it off I am going to apply the load balancing configuration, and I am going to target lbservers, do remote_user: vagrant, become: yes, roles: haproxy and then that’s really all that I need.
So now what I am going to do is I am going to save this file and I am going to execute the playbook. So remember the tasks are executed sequentially from top to bottom so the same thing will apply for the roles; basically I am going to start with the common role and it’s going to execute in order. So I am going to start with the SELinux pieces and go on to ntp and then so on and so forth. So we will kind of walk through it as it’s going through the terminal.
So I am going to go over to that; ansible-playbook –i hosts just like we have been doing, and I am going to do site.yml and run. Ok, so we are starting off here with the common role, installing the EPEL repository. Now this is going to execute on all of the hosts, so we should see an output on three. Great, and now we are moving on to the SELinux.yml which we included in that main.yml file. And so now it’s going to go through those tasks.
Okay, so that completed successfully, so now we get the OK back on the test to see if SELinux is running. Now we move on to the ntp tasks which is the second include in the common role. Great, so the install went through, you can see here we have got the handler to restart ntp at the end of that play. And so now we are then moving on the second role called which is the database role. So we should only be targeting the one host here, so only the database server.
Ok, perfect looks like everything is going through. So now we are into the starting the service and then there is the handler at the end. Great, so now we are on to the double role call, the apache first and now the web role. Ok, perfect. So here we are starting the second role call with web: install git and then we are going to move on to checking out the repo and then configuring the file as necessary. So we are just kind of hanging out here.
Ok, looks good. So remember we did the flush handler to run it right after the link app config and that’s happening. And so we got the proper response back from that URL we included, so now we are on to the last role which is the load balancing configuration. And it has completed successfully, so hurray. No errors, looks like everything is good and again just like with that principle of idempotency that I have mentioned a few times already we could run this again and then everything should return OK because we have already run it before.
And that concludes our demonstration on how to call a role inside of your main playbook. Join us in our next lesson where I’ll take you through an overview of Ansible Tower.
Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.
He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.
Jeremy holds professional certifications for both the AWS and GCP cloud platforms.