Creating a New Role
Start course
1h 27m

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 walk you through how to create a new role and add it into the existing roles that we have already created. So I am in my terminal and I am in the roles directory, and so we already added on to our apache and common roles and pulled those from the site.yml file that we created in our introduction to playbooks video. So now I want to create another role and I am going to do that with the ansible-galaxy command. So I will do ansible-galaxy init, and then I am going to make another role called web, and that was created successfully. And so, now if I do an ls I now have all three of those roles inside the roles directory.

So, I can switch back over to my text editor here and now I am going to just collapse these role directories here and you can see I have the web role underneath the roles directory, which is perfect, and basically it’s the same structure. So just like I have done before, I don’t have a playbook to base this off of already, but it’s the same procedure. 

I am going to start with the tasks, because that’s where you’re always going to start, and then I will build off from there. So I’m going to go to the main.yml file for web and the first thing that I want to do is I want to install git, because I am actually going to be pulling from our repository just a simple app that tells you what server you are on and that’s pretty much it, so install git could be my task name, then I am going to use yum module to install it and I am going to set the state to present, and so now that I have installed git, I am going to check out the app from the repo.

So checkout lameapp and that’s actually what the app name is called, I am not assigning adjectives, no judgment. So here’s where I am going to call the git module and now I am going to just type in the repo address, so and that’s the repo. Then I am going to set some other params here, I am going to set the version to a variable: “{{lameapp_version|string}}” and I’m going to cast that to a string inside and I am actually casting it to a string to prevent errors, so we are good there, and then I am going to move on to the next task.

So, now I am going to set the permissions on the app, so I am going to use the file module for that. The name is going to be set to directory /var/www/lameapp/, and then I am going to set the mode to 0755. Ok. So that’s all good to go. Now I am going to add in the apache config file because it’s all pretty much related to each other, this is building off of the apache role that we already used. So now I am going to add an apache config file and I am going to copy it -- copy module, set the source to lameapp.conf, the destination is going to be a variable, so {{sites_available}}, and then I am going to include a notify here to restart apache, so two instances in which I am actually restarting apache. And then the next task, I am going to link the app config using the file module -- see how handy it is, look at all the stuff I can do with the file module.

So linking the app config I am going to set the source to a variable: {{sites_available}}/lameapp.conf, and then I am going to set the destination to a variable: {{sites_enabled}}/lameapp.conf, and then I am going to set state to a link because I am linking them, so that should be good there. I just want to make sure, I am good, perfect. Ok. And so by setting the link the symbolic link is going to be created in this case and so I set the state to link so it’s going to link these two directories together.

And so now we are going to move on to our next task, where I am actually going to bring in the flush_handlers option, and what this does is it actually allows me to run the handler now, so if you see here in my playbook I include the restart apache under the add apache config file task. And what the flush_handlers is going to do is it’s actually going to run the handler to restart apache right now when I call this meta tag right now instead of the end of the play. So it allows us to actually kick off that restart early.

OK. And so now that I have that in there, I am going to put my last task in, I am going to check for the proper response from the UI. And I am going to use the uri module for that. Now I got a bunch of parameters, so I am going to do a key value in this format instead. So url I am setting it to http://localhost/lame and then I am going to return the content. I am going to register the output of that and I am just going to call the variable result, and then I am going to include some looping logic.

So I have an until keyword here, I am going to put until: “Hello Moon” in result.content -- would help if I actually put a dot (.) there. OK. So basically I am going to run this command. I am going to run the uri module and I am going to wait until I get Hello Moon in the output, and that’s when the loop will stop.

And so that’s pretty much it, so basically to kind of recap what I am doing in this role, I am installing git so I can checkout this repo, I am setting permissions on the app, because there is an app inside the repo, I am adding the apache config file in this case, to make sure that we are all good, then I am going to link the app config, flush the handler so it starts now, and then I am going to check the proper response from the url inside the uri module.

So, now this is complete and just like with the other ones I need to handle any remaining pieces that are missing, and I do actually need to put lameapp.conf inside of the files directory, because I am just copying it so I am not templating it out, so that’s why I should go in the files directory.

So I am going to go under the files directory and create a new file, lameapp.conf, and I am just going to copy paste that from my other monitor over here, great. Save the file then go back to my file here and the only thing I need to do is handle the handler, so restart apache. I will go to my handler file for web role, just do a quick restart apache, save the file and we are good to go. So that concludes our video on how to create a new role and insert it into an existing role structure.

Look out for our next video, where I am going to show you how to actually call all these roles from the main playbook and run them.


About the Author
Learning Paths

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, Azure, GCP), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).