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