Introduction to Ansible Transcripts
Chapter: Ansible Core Concepts
Lecture: Tasks

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Tasks are Ansible's building blocks for accomplishing your objective. Tasks are written in YAML and they are in the instructions
0:09 that invoke Ansible modules to execute an action. We took a look at how these concepts related in the first video
0:15 and we saw that there are modules for all sorts of actions we want to take like working with Git or installing and configuring Redis
0:21 and we needed a mechanism to specify how to call those modules. That's where tasks fit in. Here's what a task looks like. We start out with a -
0:29 and then a space and keyword name. And we start with a human readable name. Think of this as a comment. Technically you don't have to start
0:35 your Ansible tasks with name but as these tasks are running having a comment on every single one to explain what it's doing and why is really useful.
0:44 So the first line that we start out with on the task is a name what this task is doing, and why. In this case, we're making sure that Git is installed
0:51 on the system as this can do one of two things. First, it's going to check if Git is installed. If it's installed, it's going to say hey, we're good.
0:57 Move on to the next task. If it's not installed, it will handle installing it for you. It does this by using the apt module.
1:03 So assuming we're on an Ubuntu system or a Debian based installation that has the aptitude package manager we're using the apt module
1:10 and we're passing it three parameters. The first is the name of the package that we want to install. That would be git-core.
1:16 The state that we want it to be in. In our case, we want the git-core package to be installed. If we said state=absent that would mean we do not want
1:23 the git-core package to be installed and it would remove the package if it was installed. And the third parameter that we're passing in
1:29 is to update the cache. The cache contains a list of all the packages that can be installed and their version numbers. And we always want to grab
1:36 the very latest version of the git-core package. So this ensures that the cache is updated equivalent to running the sudo apt update command
1:44 before we check to see if git init is installed and install it. And finally, become true specifies we want to use our super user privileges
1:51 because apt requires super user privileges to install our package. That's what a task looks like written in YAML
1:56 and there's two ways we can execute this. The most common is going to be in a playbook where you have a bunch of tasks grouped together.
2:02 And the other way is we can run ad-hoc commands. So let's run a couple of those just so we can get comfortable with how to invoke modules using tasks.

Talk Python's Mastodon Michael Kennedy's Mastodon