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 a 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:18,360
So in YAML, indentation is important to identify relations between parent objects and child

17
00:01:18,360 --> 00:01:19,360
objects.

18
00:01:19,360 --> 00:01:23,279
Then we see a play header that is telling us that this playbook needs to be executed

19
00:01:23,279 --> 00:01:24,720
on 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 a 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,879
Minus use student, because we didn't specify any defaults.

28
00:02:02,879 --> 00:02:06,639
I'm giving the username of the remote user.

29
00:02:06,639 --> 00:02:11,220
And minus k will prompt for the sudo password on deploy-webserver.

30
00:02:11,220 --> 00:02:16,740
And then after entering the become password, oh no, it's telling me unable to parse Linux

31
00:02:16,740 --> 00:02:17,860
plus inventory.

32
00:02:17,860 --> 00:02:19,100
You know why?

33
00:02:19,100 --> 00:02:23,580
That is because the inventory file is not in the current directory.

34
00:02:23,580 --> 00:02:28,460
Now a simple change is to make that minus i dot dot slash inventory, because that is

35
00:02:28,460 --> 00:02:32,979
where my inventory file is.

36
00:02:32,979 --> 00:02:35,860
And now we can see that the playbook is running.

37
00:02:35,860 --> 00:02:38,100
And oh my goodness, what do we get?

38
00:02:38,100 --> 00:02:43,860
We get a couple of error messages on the remote Ubuntu machine.

39
00:02:43,860 --> 00:02:45,779
And the error messages include what?

40
00:02:45,779 --> 00:02:49,619
Well, it's telling me could not open log file and could not open log file.

41
00:02:49,619 --> 00:02:52,539
That is typically a permission-based problem.

42
00:02:52,539 --> 00:02:55,820
Let me show you what the problem is by giving you another example.

43
00:02:55,820 --> 00:03:04,100
I'm going to give you answerable all, minus i inventory, minus m command.

44
00:03:04,100 --> 00:03:09,300
Let me do a minus k as well, minus uppercase K for the become password.

45
00:03:09,300 --> 00:03:16,220
Then we use minus m command and minus a ls minus l slash root.

46
00:03:16,220 --> 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,619
So it's prompting for the become password.

48
00:03:24,619 --> 00:03:27,220
And then I'm making the same mistake again.

49
00:03:27,220 --> 00:03:29,699
Well, that should be a quick fix.

50
00:03:29,699 --> 00:03:32,380
So it's prompting for the become password.

51
00:03:32,380 --> 00:03:35,179
And then I'm getting a permission denied.

52
00:03:35,179 --> 00:03:37,059
You know what is missing here?

53
00:03:37,059 --> 00:03:42,419
What is missing is the option minus b, minus b for become.

54
00:03:42,419 --> 00:03:45,100
Minus k prompts for the become password.

55
00:03:45,100 --> 00:03:50,220
That is a pseudo password, but it won't use it without the option minus b.

56
00:03:50,220 --> 00:03:54,979
So here you can see that the ls minus l root works with the minus b.

57
00:03:54,979 --> 00:03:59,699
And we need to do something similar on this playbook.

58
00:03:59,699 --> 00:04:03,179
So we make that minus b, minus uppercase K.

59
00:04:03,179 --> 00:04:08,460
And then after entering the become password, it is gathering facts.

60
00:04:08,460 --> 00:04:13,220
That is where it's discovering properties that can be used in playbook conditionals.

61
00:04:13,220 --> 00:04:14,500
It's installing the package.

62
00:04:14,500 --> 00:04:16,660
It's starting and enabling the service.

63
00:04:16,660 --> 00:04:18,940
And now we are all done.

