4.3 Ansible Playbooks - Output
In this lab we learn how to handle output of tasks.
Task 1
- Write a playbook
output.ymlthat uses theansible.builtin.commandmodule 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
outputby using theregisterkeyword. - Include a task using the
ansible.builtin.debugmodule to print out all content of the variableoutput. If unsure, consult the documentation about theansible.builtin.debugmodule.
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.ymlusing theansible.builtin.debugmodule 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>.bakfor each file<filename.cf>that was found. Use theansible.builtin.commandmodule. 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.ymlto only create the backup if no backup file is present. - Solve this task by searching for files ending with
.bakand registering the result to a variable. Then do tasks only if certain conditions are met.
Tip
Have a look at the documentation about the ansible.builtin.command module: Ansible Docs - commandSolution Task 3
Possible solution 1:
Example output.yml:
| |
Possible solution 2:
Example output.yml:
| |
Task 4 (Advanced)
- Ensure
httpdis stopped on the groupwebby using an Ansible ad hoc command. - Write a play
servicehandler.ymlthat does the following: - Install
httpdby using theansible.builtin.dnfmodule - Start the service
httpdwith theansible.builtin.commandmodule. Don’t useansible.builtin.serviceoransible.builtin.systemd_servicemodule. - Start the service only if it is not started and running already.
(The output of
systemctl status httpddoesn’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 anyway.
Solution Task 4
Stop the httpd service with Ansible:
| |
Content of servicehandler.yml:
| |
Task 5 (Advanced)
- Rewrite the playbook
servicehandler.ymland ensure that theignore_errors: trueline is removed. Instead, set the state of the task to failed if and only if the output ofsystemctl status httpdcontains 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.confand backup the file before. Use theansible.builtin.copymodule to do this in ad hoc command. - Restart
httpdby 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
httpdonnode1and 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 September 26, 2025: Update dependency husky to v9 (#248) (71c506c)