Introduction to Ansible Transcripts
Chapter: Running Playbooks
Lecture: Creating Our First Role
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay so we've now got our SSH key we've got a server provisions we have a hosts file and the beginning of our playbook. But our playbook needs tasks
0:08
that are stored in roles in order to have something to do. So right now we have the following files: we got our private key
0:14
public key, host file, playbook.yml. We're going to create a directory within our project directory.
0:19
We'll call this roles and we'll store every single role that we create for this playbook. Now in this playbook we're only going to have a single role
0:25
and we'll just call that common. So when you go into the roles directory create another role called common. And common is typically used for roles that
0:34
cut across every single on of your servers. You'll usually want to give your roles more descriptive names like web server or data base server.
0:40
When there expected that there only going to apply to certain types of servers. But I typically use common when I know a task is
0:46
going to apply to every single server that I want to use that playbook for. All right so within roles/common create a directory named tasks.
0:53
And this will store our yml files with each of our tasks. Now there is a specific file naming convention we need to use here.
0:59
And that is, there needs to be a main.yml file so that Ansible automatically picks up what tasks need to be executed.
1:06
Think of main.yml as the starting place for the role. So create a new file named main.yml and we can give this just a brief comment.
1:17
And then we're going to import other files that have individual tasks. Unless your role is only going to
1:22
execute one or two tasks you're typically going to want to split out your tasks into multiple files. The way that you'll handle that is with the
1:29
include syntax. In our case, we're just going to have our first file be ping.yml. So that's it for main.yml so far.
1:36
Only one usable line, that is the include line for ping.yml. But of course we need to create ping.yml So create a new file named ping.yml
1:47
And the ping command just tries to connect to a remote server and sees if its operational and we can actually connect to it and its responding to us.
1:57
And this is going to ping whatever servers that we have listed in our inventory file. And save ping.yml and we're going to try and run this playbook.
2:05
Now you can't run it from within the task directory. So go back into the base directory for first playbook. Should be at the top level directory
2:13
of the project directory. Now we're going to try to run our playbook. The way we do this is with the ansible-playbook command.
2:19
So if you just try to type in ansible-playbook it'll tell you all the options the arguments we can pass in. We are going to pass in a few arguments.
2:27
We'll specify ansible-playbook and then an inventory file. That inventory file is the one that's in our current directory and that's hosts.
2:34
We're going to specify a certain private key and that is first_playbook. And then we have our playbook.yml file. Question is whether this will work
2:43
just the first time we try to execute it with the single ping command that we've put in place. And unfortunately it failed on the first run.
2:50
Now I actually wanted to show this error because this is something that will pop up frequently if you are working with new servers.
2:55
And here's what's happening if you see this issue where there was a module failure specifically, you see this particular key
3:01
which is usr/bin/Python not found. What's happening here is that there is no Python version 2 installed on that remote server.
3:09
So Ansible is trying to execute commands on a remote server using the Python command but only Python 3 is installed not Python 2.
3:18
So we need to tell Ansible don't try to use Python 2 we explicitly want to use Python 3 on remote servers or on specific remote servers that we would
3:27
specify in our hosts file. So here's how we do that and how we can fix this problem. Open up the hosts file and right next to the IP address
3:36
were going to specify the particular location and file name of the Python interpreter that we want to use on that server.
3:44
So by default it was trying usr/bin/Python but we want to specify ansible_Python_interpreter=/usr/bin/Python3. Save that file.
3:56
And lets give our playbook a try again. Okay this time it was able to execute using Python 3 on the remote server and it looks like
4:06
everything ran properly. We were able to ping that remote server. And this is a good start to our Ansible playbook.