While automating a large number of systems there are various tasks that are same in many systems but there are also tasks that are different in many systems, combining both possibilities we get a diversity of playbooks that should be run to do our required setup. And in these playbooks tasks are written again and again with some tasks same some tasks different which is a lot of work for a developer. So what we do is we create a role for a particular set of tasks that are always done together and then use these roles in required playbook. This way we don’t need to write code again and again.

Today we are going to create two roles for webserver and loadbalancer then create a playbook to use these Roles and create a Load balanced setup.

To create a Role we need to run :

ansible-galaxy role init role_name

we can see our roles list using command :

ansible-galaxy role list — roles-path path_where_roles_are_created

Then we add these roles path to our inventory file :

We can see that inside roles there are various directories that contain your various types of data used in a set of tasks.

Now inside tasks directory of our role (here myapache) there is a main.yml file already present where you write all the tasks to be done.

Also in vars folder we write variables to be used in tasks.

Similarly goes for loadbalancer roles :

Now we write our main playbook whicb will use roles to do the setup.

Now when we run this playbook :

we can see all the task at hand are done and our setup is ready.

That’s it.

Thank you.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store