0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Creating Tasks for our Common Role


Q&A (0)

Notes (0)

Resources (0)

Saving Progress...


There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, we are going to define a task for our common role. This task will configure the server to update and upgrade the packages on the host to ensure the server is updated every time the playbook is run.

Defining Tasks our Common Role

As we have discussed in previous lessons, Ansible will expect the tasks for our Ansible role to be defined in a file called main.yml inside of the tasks directory. Let's go ahead and create that file now:

touch ~/code/roles/common/tasks/main.yml

Next, since our Linux servers use the apt package manager, we want to find a module that works with apt. We can search our modules by using using ansible-doc -l and piping that to grep, with an pattern of '\.apt'.

ansible-doc -l | grep '\.apt'

You could also simply grep for '.apt', but grep will interpret the period (.) as a wildcard like symbol, so we need to escape it with a backslash.

Another way would be to filter the namespaces down to ansible.builtin and grep for apt:

ansible-doc -l ansible.builtin | grep apt
Listing All apt modules with ansible-doc
Listing All apt modules with ansible-doc

Take a look at the help for the apt module:

ansible-doc ansible.builtin.apt

We want to look for the update_cache and upgrade options particularly. For the upgrade option, we want to specify the option dist which is the apt package managers way of smartly upgrading the system. When you're done, press q to exit the documentation.

Now that we understand the module and parameters we want to set in our playbook, let's make the playbook. Open the playbook in your favorite editor:

nano ~/code/roles/common/tasks/main.yml

Add the following content:

# tasks file for common
- name: Update all packages to the latest version
    update_cache: yes
    upgrade: dist
  become: yes

This task ensures that all the packages on the server are updated to their latest versions.

You will notice we don't need to define the hosts or prefix out tasks with the tasks directive, instead we just start listing the tasks for this role. This is because Ansible already knows (due to the folder structure and file naming) that this is a set of tasks for a particular role.

Now if I tree my home directory, I now see the following output:

paulh@ansible-controller:~/code$ tree
├── ansible.cfg
├── ansible.cfg.example
├── first_playbook.yml
├── inventory
└── roles
    └── common
        ├── defaults
        ├── files
        ├── handlers
        ├── meta
        ├── tasks
        │   └── main.yml
        ├── templates
        └── vars

9 directories, 5 files

That looks good! We are done adding our task to this role.


That is all we need to do to define our Ansible Role task! Great job and I'll see you in the next lesson!

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Notify of
profile avatar
Inline Feedbacks
View all comments