Ansible: Defining “Common Role” and Writing “Ansible playbook” to install Packages

Share At:

In continuation to my previous article titled “Ansible: Configuring Ansible Server — Client Infrastructure”, here we are going to see how to define “Common role” and write an ansible playbook to install packages on client servers.


  1. In this demonstration, we will be using centos-07. Please go through the Ansible documentation for supported OS platforms.
  2. We will be using 3 machines for our lab: 1 ansible workstation and 2 ansible clients with the below IP details:

Note: Ansible workstation would be our ansible server.

3. For all the servers — memory should be at least 2 GB and there should be at least 2 core CPU.

4. Ansible workstation and client servers should be able to communicate with each other over ssh (port 22).

5. We have already installed the ansible package on ansible workstation and have created an inventory file — /etc/ansible/hosts.

The inventory file “/etc/ansible/hosts” should have entry like below:

Defining Common Role and Writing Playbook:

Roles are, at the most basic level, groups of tasks organized by their purpose.

Here we will create a “common” role that installs all the base packages we would be using on all our managed servers, such as git, vim, etc…

Please perform the below steps:

  1. Create Directory structure something like below:

2. Now go to “/home/vagrant/repo/ansible-automation/roles/common/tasks” and create two files called “main.yml” and “common_pkg.yml”.

Ansible is going to read main.yml at first.

3. Now update main.yml something like below:

4. Update common_pkg.yml something like that:

5. Now we are going to create a playbook called “webserver.yml” which will use the common role we have just created.

For this, go to the “/home/vagrant/repo/ansible-automation” directory and create “webservers.yml” file. Update the webservers.yml file to look something like below:

6. The entire directory structure will be something like this:

7. Use “ansible-playbook webservers.yml — check” to perform the dry runThis will not perform the actual install on client servers.

8. Now run “ansible-playbook webservers.yml” command to run the playbook and perform actual install on client servers:

9. Now login to the client-server and validate if the packages have been installed.

That’s It. We are done.

Happy Learning !!!

Share At:
0 0 votes
Article Rating
Notify of
1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
Бонус при регистрации на binance

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

Back To Top

Contact Us