4.3 Ansible Playbooks - Output
In this lab we learn how to handle output of tasks.
Task 1
- Write a playbook
output.yml
that uses thecommand
module to find all config files of postfix. These files are located under/etc/postfix/
and end with.cf
. Targeted server isnode1
. - Register the result to a variable called
output
by using theregister
keyword. - Include a task using the
debug
module to print out all content of the variableoutput
. If unsure, consult the documentation about thedebug
module.
Note
You might need to install thepostfix
package on node1
.Solution Task 1
Documentation about debug module
Example output.yml
:
|
|
Task 2
- Add another task to the playbook
output.yml
using thedebug
module and print out the resulting filenames of the search above.
Tip
Use an appropriate return value to show the output. Information about return values can be found here: Ansible Docs - Common Return Values- Now, loop over the results and create a backup file called
<filename.cf>.bak
for each file<filename.cf>
that was found. Use thecommand
module. Remember, that the result is probably a list with multiple elements.
Solution Task 2
Example output.yml
:
|
|
Task 3 (Advanced)
- Now we enhance our playbook
output.yml
to only create the backup if no backup file is present. - Solve this task by searching for files ending with
.bak
and registering the result to a variable. Then do tasks only if certain conditions are met.
Tip
Have a look at the documentation about the command module: Ansible Docs - commandSolution Task 3
Possible solution 1:
Example output.yml
:
|
|
Possible solution 2:
Example output.yml
:
|
|
Task 4 (Advanced)
- Ensure
httpd
is stopped on the groupweb
by using an Ansible ad hoc command. - Write a play
servicehandler.yml
that does the following: - Install
httpd
by using thednf
module - Start the service
httpd
with thecommand
module. Don’t useservice
orsystemd
module. - Start the service only if it is not started and running already. (The output of
systemctl status httpd
doesn’t contains the stringActive: active (running)
)
Note
Have a look at the documentation about conditionals: Ansible Docs - Playbook Conditionals
systemctl status
returns status failed
when a service is not running. Therefore we use ignore_errors: true
in the corresponding task to let Ansible continue anyways.
Solution Task 4
Stop the httpd
service with Ansible:
|
|
Content of servicehandler.yml
:
|
|
Task 5 (Advanced)
- Rewrite the playbook
servicehandler.yml
and ensure that theignore_errors: true
line is removed. Instead set the state of the task to failed when and only when the output ofsystemctl status httpd
contains the string “failed”.
Note
Have a look at the documentation about error handling: Ansible Docs - Playbooks Error Handling- Rerun your playbook and ensure it still runs fine.
- By using an ansible ad hoc command, place an invalid configuration file
/etc/httpd/conf/httpd.conf
and backup the file before. Use thecopy
module to do this in ad hoc command. - Restart
httpd
by using an Ansible ad hoc command. This should fail since the config file is not valid. - Rerun your playbook and ensure it fails.
- Fix the errors in the config file, restart
httpd
onnode1
and rerun your playbook. Everything should be fine again.
Solution Task 5
Example servicehandler.yml
:
|
|
|
|
Now fix your apache config. You could use the backup of the file created in the previous ad-hoc command.
|
|
All done?
Last modified December 6, 2024: Remove submodules remark from README (#219) (769b6c5)