Creating Tasks for our Common Role
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
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.
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
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
apt:
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.
Conclusion
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.