Red Hat Certified Engineer RHCE · Free Practice Question Hard
Question 12
You need to configure firewall rules on managed nodes to ensure they persist after a reboot. How can you use Ansible's firewalld and template modules to deploy and enable customized firewall configurations that remain persistent across reboots?
-
A
- - hosts: all
- tasks:
- - name: Deploy custom rules
- ansible.builtin.template:
- src: templates/firewall.j2
- dest: /etc/firewalld/services/custom_rules.xml
- mode: '0644'
- - name: Apply rules
- ansible.builtin.firewalld:
- service: custom_rules
- permanent: yes
- state: enabled
- - name: Reload firewalld
- ansible.builtin.service:
- name: firewalld
- state: reloaded
This ensures firewall rules persist after reboots.
-
B
This is a performance based question and not multiple choice. Therefore the answer is in #1. If you require additional explanation, please ask your question in the Q&A section.
Reveal correct answer
Correct answer: A
A.
The answer utilizes Ansible's firewalld and template modules to configure firewall rules that persist across reboots. First, the template module is used to deploy a customized firewall configuration file (custom_rules.xml) generated from a Jinja2 template. The file is placed in the appropriate directory (/etc/firewalld/services/). Then, the firewalld module is used to apply the custom rules and ensure they are permanent, meaning they will persist after a reboot. Lastly, the firewall is reloaded to apply the new rules and ensure they take effect.
Line-by-Line Explanation
- hosts: allThis line defines that the tasks should run on all managed nodes specified in the inventory.
tasks:This section contains the tasks that will be executed on the managed nodes.
- name: Deploy custom rulesThis task name indicates that the task will deploy custom firewall rules to the managed nodes.
ansible.builtin.template:This uses the
templatemodule to manage files based on Jinja2 templates. The file is created dynamically with content from the specified template.
src: templates/firewall.j2The source file,
firewall.j2, is a Jinja2 template stored in thetemplatesdirectory. This file contains the firewall configuration template that will be processed and deployed.
dest: /etc/firewalld/services/custom_rules.xmlThis defines the destination path where the generated configuration file will be saved on the managed nodes. It will be placed in
/etc/firewalld/services/with the namecustom_rules.xml.
mode: '0644'This specifies the file permissions for the
custom_rules.xmlfile, giving read and write permissions to the owner, and read permissions to the group and others.
- name: Apply rulesThis task name indicates that it will apply the custom firewall rules after they are deployed.
ansible.builtin.firewalld:This uses the
firewalldmodule to manage firewall rules.
service: custom_rulesThis specifies that the custom rules file (
custom_rules.xml) should be applied by thefirewalldservice.
permanent: yesThis ensures that the rules are permanent, meaning they will persist after a system reboot.
state: enabledThis ensures that the firewall service is enabled and the rules are applied.
- name: Reload firewalldThis task name indicates that the firewall service will be reloaded to apply the new rules.
ansible.builtin.service:This uses the
servicemodule to manage services on the managed nodes.
name: firewalldThis specifies that the
firewalldservice should be reloaded.
state: reloadedThis reloads the
firewalldservice to apply the new firewall rules.
This configuration ensures that the firewall rules are deployed, applied, and persist across reboots by utilizing Ansible's firewalld and template modules.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
