4. Ansible Playbooks - Basics
In this lab we’ll get used to writing and running Ansible playbooks.
Task 1
Create a playbook webserver.yml
which does the following:
- Install
httpd
on the nodes in theweb
group. - Start
httpd
and ensure the service starts on boot. Ensure that the Linux firewall is also started and enabled. - Ensure port 80 is open on the firewall.
Tip
Check what the optionsimmediate
and permanent
of the firewalld
module mean and do.- Run the playbook. After completion, test if the
httpd.service
is running and enabled onnode1
.
Solution Task 1
Below is a possible solution for your playbook:
|
|
Run your playbook with:
|
|
Check httpd.service
on group web
:
|
|
Hint
The ports for ssh, dhcp and cockpit are opened by default in the firewalld. It is best, especially for documentation, to open the ports explicitly in a basic settings file.
Task 2
- Create a folder
inventory
and move your inventoryhosts
there. - Configure Ansible to use
/home/ansible/techlab/inventory/hosts
as the default inventory. Do this using a configuration file in the/home/ansible/techlab/
directory. - Run the playbook again without using the
-i
flag to see if the configuration works.
Solution Task 2
Copy the default ansible.cfg to your directory:
|
|
Edit your ansible.cfg
file. Uncomment and edit the “inventory” entry to use your file:
[defaults]
# some basic default values...
inventory = /home/ansible/techlab/inventory/hosts # <-- edit this line
#library = /usr/share/my_modules/
|
|
Task 3
- Intentionally add errors to your playbook and have a look at the output. You should get a feeling for Ansible’s error messages:
- Add a wrong indentation. Remember that this is a common mistake!
- Use a tab character for identation. Some editors do that automatically.
- Add a wrong parameter name.
- Remove the mistakes.
Solution Task 3
Wrong intendation:
|
|
Wrong parameter name:
|
|
Task 4
- Create a playbook
tempfolder.yml
- The playbook
tempfolder.yml
should create a temporary folder/var/tempfolder
on all servers except those in the groupdb
.
Tip
Take a look at the user guide and find out how to use more complex inventory patterns. See Ansible Docs - User Guide- The folder has to have the sticky bit set, so that only the owner (set owner/group to
ansible
) of the content (or root) can delete the files. - Run the playbook and then check if the sticky bit was set using an ad hoc command.
Solution Task 4
|
|
Note
ansible-doc file
doesn’t provide any information about setting special permissions like sticky bit (man chmod
will help you though). Remember to use a leading 0 before the actual permissions.