1 00:00:00,000 --> 00:00:09,760 In this video, you'll learn about Ansible playbooks. 2 00:00:09,760 --> 00:00:14,600 Playbooks provide a DevOps way for working with Ansible, for the simple reason that they 3 00:00:14,600 --> 00:00:17,520 define the desired state in YAML. 4 00:00:17,520 --> 00:00:20,639 And that's an example of configuration as code. 5 00:00:20,639 --> 00:00:25,040 The Ansible playbook command is used to compare the current state of the managed machine with 6 00:00:25,040 --> 00:00:31,040 the desired state, and if they don't match, the desired state is implemented. 7 00:00:31,040 --> 00:00:39,880 Use for instance the command ansible-playbook-i-inventory-u-student-k deploy-webserver.yaml. 8 00:00:39,880 --> 00:00:44,439 You can find this deploy-webserver.yaml in the course Git Repository. 9 00:00:44,439 --> 00:00:46,619 So let me show you. 10 00:00:46,619 --> 00:00:50,720 To access the playbook, we need access to the course Git Repository. 11 00:00:50,720 --> 00:01:00,560 Use git clone to https://github.com/.sandervanvugt/.linux+. 12 00:01:00,560 --> 00:01:03,180 Then you have a directory with the name linux+. 13 00:01:03,180 --> 00:01:07,199 And in that directory, use deploy-webserver.yaml. 14 00:01:07,199 --> 00:01:09,080 So what is this doing? 15 00:01:09,080 --> 00:01:12,260 This is a playbook written in YAML. 16 00:01:12,260 --> 00:01:17,959 So in YAML, indentation is important to identify relations between parent objects and child 17 00:01:17,959 --> 00:01:19,239 objects. 18 00:01:19,239 --> 00:01:23,519 And we see a play header that is telling us that this playbook needs to be executed on 19 00:01:23,519 --> 00:01:24,720 all hosts. 20 00:01:24,720 --> 00:01:31,440 Then we see a list of tasks, which is identifying the package task where we want to install 21 00:01:31,440 --> 00:01:34,300 the latest version of the Apache 2 package. 22 00:01:34,300 --> 00:01:40,000 And the service task is calling the service module to make sure that the Apache 2 service 23 00:01:40,000 --> 00:01:45,639 will reach the state of started and will also be enabled to ensure that it will automatically 24 00:01:45,639 --> 00:01:49,680 be started if the target machine reboots. 25 00:01:49,680 --> 00:01:53,160 Then I'm using answerable playbook, minus i inventory. 26 00:01:53,160 --> 00:01:59,400 No need to specify any host names, because that is in the playbook itself. 27 00:01:59,400 --> 00:02:02,839 Minus you, student, because we didn't specify any defaults. 28 00:02:02,839 --> 00:02:06,639 I'm giving the username of the remote user. 29 00:02:06,639 --> 00:02:11,199 And minus k will prompt for the sudo password on deploy-webserver. 30 00:02:11,199 --> 00:02:16,720 And then after entering the become password, oh no, it's telling me unable to parse Linux 31 00:02:16,720 --> 00:02:17,839 plus inventory. 32 00:02:17,839 --> 00:02:19,080 You know why? 33 00:02:19,080 --> 00:02:23,559 That is because the inventory file is not in the current directory. 34 00:02:23,559 --> 00:02:28,479 Now a simple change is to make that minus i dot dot slash inventory, because that is 35 00:02:28,479 --> 00:02:32,960 where my inventory file is. 36 00:02:32,960 --> 00:02:35,860 And now we can see that the playbook is running. 37 00:02:35,860 --> 00:02:38,080 And oh my goodness, what do we get? 38 00:02:38,080 --> 00:02:43,880 We get a couple of error messages on the remote Ubuntu machine. 39 00:02:43,880 --> 00:02:45,800 And the error messages include what? 40 00:02:45,800 --> 00:02:49,600 Well, it's telling me could not open log file and could not open log file. 41 00:02:49,600 --> 00:02:52,539 That is typically a permission-based problem. 42 00:02:52,539 --> 00:02:55,800 Let me show you what the problem is by giving you another example. 43 00:02:55,800 --> 00:03:04,080 I'm going to give you answerable all minus i inventory minus m command. 44 00:03:04,080 --> 00:03:09,279 Let me do a minus k as well, minus uppercase K for the become password. 45 00:03:09,279 --> 00:03:16,199 Then we use minus m command and minus a ls minus l slash root. 46 00:03:16,199 --> 00:03:21,679 I like this example to figure out if I have any permission issues. 47 00:03:21,679 --> 00:03:24,600 So it's prompting for the become password. 48 00:03:24,600 --> 00:03:27,279 And then I'm making the same mistake again. 49 00:03:27,279 --> 00:03:29,679 Well, that should be a quick fix. 50 00:03:29,679 --> 00:03:32,360 So it's prompting for the become password. 51 00:03:32,360 --> 00:03:35,119 And then I'm getting a permission denied. 52 00:03:35,119 --> 00:03:37,039 You know what is missing here? 53 00:03:37,039 --> 00:03:42,559 What is missing is the option minus b, minus b for become. 54 00:03:42,559 --> 00:03:45,080 Minus k prompts for the become password. 55 00:03:45,080 --> 00:03:50,039 That is a pseudo password, but it won't use it without the option minus b. 56 00:03:50,039 --> 00:03:54,720 So here you can see that the ls minus l root works with the minus b. 57 00:03:54,720 --> 00:03:59,679 And we need to do something similar on this playbook. 58 00:03:59,679 --> 00:04:03,160 So we make that minus b, minus uppercase K. 59 00:04:03,160 --> 00:04:08,440 And then after entering the become password, it is gathering facts. 60 00:04:08,440 --> 00:04:13,199 That is where it's discovering properties that can be used in playbook conditionals. 61 00:04:13,199 --> 00:04:14,479 It's installing the package. 62 00:04:14,479 --> 00:04:16,640 It's starting and enabling the service. 63 00:04:16,640 --> 00:04:17,720 And now we are all done.