Configuration Management

Amitt AshokAmitt Ashok
2 min read

Ansible Role

After understanding inventory and playbooks, let's dive deeper into more concepts related to Ansible.

In a previous article, we saw how to create a playbook for the simple installation and running of an HTTPD server. Imagine we have five different servers with different operating systems, and we need to install Kubernetes on each one. In this case, our playbook would have over 60 tasks and more than 200 lines of code.

It would be a nightmare if someone wants to review that 200+ lines of code with 60+ tasks. To simplify this, the concept of an Ansible role comes into play.

Ansible roles simplify the process, provide modularity, and allow you to share created roles easily with the community and teammates.

What exactly does an Ansible role do? Instead of writing all the code in a single playbook file, it lets you write code for each component separately. Ansible then picks up each component during execution.

With Ansible roles, you don't need to create any files or folders manually; they are created automatically. Let's understand with execution.

amitt-ashok@ubuntu:~/Ansible$ ls
first-playbook.yaml  index.html  inventory.ini  playbook.yaml  README.md
amitt-ashok@ubuntu:~/Ansible$

We can check here there is no folder created now, once you hit this command ansible-galaxy role init <role-name>

ansible-galaxy role init demo
amitt-ashok@ubuntu:~/Ansible$ cd demo/
amitt-ashok@ubuntu:~/Ansible/demo$ tree
.
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

With an Ansible role, different folders and files are created automatically, which are necessary to structure the playbook properly. This is the beauty of the ansible role. Now we can write code inside the file according to our requirement

Ansible-galaxy:

Ansible-galaxy is a registry where community members or teammates share their created roles for easy access by others. We can pull roles from Galaxy and push our created roles to Galaxy.

https://galaxy.ansible.com/ui/

You can visit the URL above to review the details.

To summarize this article, we covered what a role is and its importance when writing large codebases. We also looked at what the Ansible Galaxy is and its significance.

Thank you for the day...

See You Soon...

1
Subscribe to my newsletter

Read articles from Amitt Ashok directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amitt Ashok
Amitt Ashok

Hey Hi there, Amit here... I love to explore new things and learn new technology.