5.1 Ansible Roles - Handlers and Blocks
We start to use handlers and blocks as well.
Task 1
- Create a playbook
myhandler.yml
which applies a rolehandlerrole
onnode2
. - The role
handlerrole
should do the following: - Create a directory
newdir
in the folder/home/ansible
. - If the folder didn’t exist before, then do create a file
README.TXT
in this folder containing the text “This folder was created at<timestamp>
”. - The value of
<timestamp>
should contain a quite accurate timestamp of whenansible-playbook
was run. - Run the playbook several times to see if it is really idempotent.
Solution Task 1
Below is a possible solution:
|
|
Task 2
- Write a playbook
download.yml
which runs a roledownloader
onnode2
. - The role
downloader
should try to download a file from a non-existing URL. - The role should be able to handle errors. Let it write the message “Download failed!” to standard output if the download task failed. The playbook must keep on running and shall not exit after this message.
- In all cases, output a message at the end of the play informing that the download attempt has finished.
- Use a
block:
to do these tasks. - Run the playbook
download.yml
.
Solution Task 2
|
|
Note
Note the failed download task, but the playbook finished nonetheless.
All done?
Last modified December 6, 2024: Remove submodules remark from README (#219) (769b6c5)