In this video, you'll learn about Ansible playbooks.
Playbooks provide a DevOps way for working with Ansible, for the simple reason that they
define the desired state in YAML.
And that's an example of configuration as code.
The Ansible playbook command is used to compare the current state of the managed machine with
the desired state, and if they don't match, the desired state is implemented.
Use for instance the command ansible-playbook-i-inventory-u-student-k deploy-webserver.yaml.
You can find this deploy-webserver.yaml in the course Git Repository.
So let me show you.
To access the playbook, we need access to the course Git Repository.
Use git clone to https://github.com/.sandervanvugt/.linux+.
Then you have a directory with the name linux+.
And in that directory, use deploy-webserver.yaml.
So what is this doing?
This is a playbook written in YAML.
So in YAML, indentation is important to identify relations between parent objects and child
objects.
And we see a play header that is telling us that this playbook needs to be executed on
all hosts.
Then we see a list of tasks, which is identifying the package task where we want to install
the latest version of the Apache 2 package.
And the service task is calling the service module to make sure that the Apache 2 service
will reach the state of started and will also be enabled to ensure that it will automatically
be started if the target machine reboots.
Then I'm using answerable playbook, minus i inventory.
No need to specify any host names, because that is in the playbook itself.
Minus you, student, because we didn't specify any defaults.
I'm giving the username of the remote user.
And minus k will prompt for the sudo password on deploy-webserver.
And then after entering the become password, oh no, it's telling me unable to parse Linux
plus inventory.
You know why?
That is because the inventory file is not in the current directory.
Now a simple change is to make that minus i dot dot slash inventory, because that is
where my inventory file is.
And now we can see that the playbook is running.
And oh my goodness, what do we get?
We get a couple of error messages on the remote Ubuntu machine.
And the error messages include what?
Well, it's telling me could not open log file and could not open log file.
That is typically a permission-based problem.
Let me show you what the problem is by giving you another example.
I'm going to give you answerable all minus i inventory minus m command.
Let me do a minus k as well, minus uppercase K for the become password.
Then we use minus m command and minus a ls minus l slash root.
I like this example to figure out if I have any permission issues.
So it's prompting for the become password.
And then I'm making the same mistake again.
Well, that should be a quick fix.
So it's prompting for the become password.
And then I'm getting a permission denied.
You know what is missing here?
What is missing is the option minus b, minus b for become.
Minus k prompts for the become password.
That is a pseudo password, but it won't use it without the option minus b.
So here you can see that the ls minus l root works with the minus b.
And we need to do something similar on this playbook.
So we make that minus b, minus uppercase K.
And then after entering the become password, it is gathering facts.
That is where it's discovering properties that can be used in playbook conditionals.
It's installing the package.
It's starting and enabling the service.
And now we are all done.