Creating Tasks for our Common Role
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
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:
No comments yet. Add the first comment to start the discussion.