Orchestrating Network Security: Comprehensive Strategies for Disabling and Managing Firewalld via Ansible

The administration of network security in Red Hat Enterprise Linux (RHEL) and its derivatives often revolves around the management of the firewalld daemon. While the primary objective of any security professional is to maintain a restrictive and well-defined perimeter, there are specific architectural scenarios where the native firewalld service must be disabled. This may occur during the migration to a more granular packet filtering system such as nftables, during the implementation of a centralized hardware firewall that renders host-based filtering redundant, or within isolated containerized environments where the overhead of a daemonized firewall is unnecessary. To achieve this at scale, Ansible serves as the primary engine for configuration management, ensuring that the state of the firewall is consistent across hundreds or thousands of nodes. The process of disabling firewalld is not merely a matter of stopping a service; it involves a multi-layered approach encompassing service cessation, the prevention of automatic restarts, and the absolute masking of the service to prevent third-party dependencies from inadvertently triggering its execution.

The Technical Architecture of Firewalld Deactivation

To understand the process of disabling firewalld through Ansible, one must first comprehend the systemic layers involved in the Linux service lifecycle. In a modern systemd-based environment, a service can exist in several states of "inactivity." A service that is simply stopped is still enabled for boot. A service that is disabled will not start at boot but can still be started manually or by another service. A masked service, however, is linked to /dev/null, making it impossible to start, even manually, until it is unmasked.

The Three-Tiered Shutdown Process

The total eradication of firewalld activity requires three distinct actions, which can be mapped to specific Ansible modules and parameters.

  1. Immediate Cessation: The service must be stopped in the current runtime. This is achieved by setting the state to stopped.
  2. Boot-time Prevention: The service must be disabled so that the systemd manager does not initiate the process during the boot sequence. This is achieved by setting enabled to no.
  3. Absolute Masking: To prevent other services—which may have a dependency on the D-Bus interface or other system components—from triggering firewalld, the service must be masked. Masking is the strongest form of disabling available in systemd.

Implementing the Deactivation Playbook

The following technical implementation demonstrates the professional method for ensuring firewalld is completely neutralized across a fleet of servers.

yaml - name: Complete Firewalld Deactivation hosts: all become: true tasks: - name: Stop firewalld and prevent its use ansible.builtin.systemd: name: firewalld enabled: no masked: yes state: stopped when: - ansible_facts.services["firewalld.service"] is defined

Deep Drilling: Analysis of the Deactivation Logic

  1. Direct Fact: The ansible.builtin.systemd module is used to manage the firewalld service.
  2. Technical Layer: The module interacts with the systemd manager. By specifying enabled: no, Ansible removes the symbolic links that tell systemd to start the service at boot. By specifying masked: yes, Ansible creates a symbolic link from the service unit file to /dev/null.
  3. Impact Layer: This ensures that no accidental systemctl start firewalld command from a junior administrator or a rogue script can re-enable the firewall, which would otherwise lead to immediate connectivity loss for applications that are not explicitly permitted in the firewall rules.
  4. Contextual Layer: This action is often a prerequisite for the deployment of nftables. If firewalld remains active, it will conflict with raw nftables rulesets by overwriting the tables and chains used by the kernel.

Transitioning from Firewalld to Nftables

In many high-performance environments, administrators choose to replace the firewalld daemon with a static nftables configuration. This transition allows for a "single source of truth" in the form of a configuration file rather than a stateful daemon.

The Migration Workflow

The migration involves a sequence of steps where firewalld is decommissioned and nftables is commissioned.

```yaml
- name: Deploy custom nftables ruleset
ansible.builtin.copy:
content: "{{ foundruleset }}"
dest: "/etc/nftables/{{ site_name }}.nft"
owner: root
group: root
mode: '0600'

  • name: Include our ruleset in configuration
    ansible.builtin.lineinfile:
    path: /etc/sysconfig/nftables.conf
    regexp: '^include "/etc/nftables/{{ sitename }}.nft"'
    line: 'include "/etc/nftables/{{ site
    name }}.nft"'

  • name: Ensure that nftables runs with our ruleset
    ansible.builtin.systemd:
    name: nftables
    enabled: yes
    masked: no
    state: restarted
    when:

    • foundruleset|length > 0
      ```

Technical Analysis of the Nftables Integration

  • The use of ansible.builtin.copy ensures that the specific ruleset for a site is placed in /etc/nftables/ with strict permissions (0600), preventing non-privileged users from reading the security logic.
  • The ansible.builtin.lineinfile module is used to modify /etc/sysconfig/nftables.conf. This is a critical administrative step because the nftables service reads this configuration file to determine which rulesets to load into the kernel upon startup.
  • The ansible.builtin.systemd module then transitions the system from a firewalld-managed state to an nftables-managed state by enabling and restarting the nftables service.

Alternative: Granular Management Instead of Disabling

In cases where disabling the firewall is not an option, Ansible provides the ansible.posix.firewalld module to manage rules programmatically. This allows for a "Single Source of Truth" where the desired state of the firewall is defined in code rather than manually entered into the CLI.

Management Comparison: Manual vs. Ansible

Feature Manual CLI (firewall-cmd) Ansible (ansible.posix.firewalld)
Consistency Prone to human error across nodes Guaranteed identical state across fleet
Traceability No record of who changed what Git-versioned playbooks
Speed Slow (node by node) Rapid (parallel execution)
Persistence Requires --permanent flag Managed via permanent: true parameter

Implementation of Rule-Based Management

To manage firewalld without disabling it, administrators can use the following patterns to ensure only necessary services are active.

```yaml
- name: "Make sure SSH is enabled in FirewallD"
ansible.posix.firewalld:
service: ssh
permanent: yes
immediate: yes
state: enabled

  • name: "Disable all other predefined FirewallD services"
    ansible.posix.firewalld:
    service: "{{ item }}"
    permanent: yes
    immediate: yes
    state: disabled
    with_items:
    • cockpit
    • dhcpv6-client
    • samba-client
    • mdns

      ```

Deep Drilling: The immediate and permanent Parameters

  1. Direct Fact: The permanent and immediate flags are used in the ansible.posix.firewalld module.
  2. Technical Layer: In firewalld, there are two distinct configurations: the runtime configuration and the permanent configuration. A rule added with permanent: true is written to disk but does not take effect until the firewall is reloaded. A rule with immediate: true is applied to the current running session.
  3. Impact Layer: If an administrator sets permanent: true but forgets immediate: true, the server will remain blocked until the next reboot or manual reload, potentially causing an outage. Conversely, using only immediate: true means the rule will vanish after a reboot.
  4. Contextual Layer: This duality is why the Ansible module explicitly provides both options, ensuring that the rule is both active now and active after a system restart.

Advanced Zone and Interface Configuration

Firewalld organizes network security into "Zones." When disabling or configuring firewalld via Ansible, it is essential to manage these zones to avoid accidental lockouts.

Zone Management Logic

Administrators can create custom zones for specific application tiers, such as an internal-apps zone, to isolate traffic.

```yaml
- name: Create a custom zone for the internal network
ansible.posix.firewalld:
zone: internal-apps
state: present
permanent: true
notify: Reload firewalld

  • name: Assign interface to internal zone
    ansible.posix.firewalld:
    zone: internal-apps
    interface: eth1
    permanent: true
    state: enabled
    immediate: true
    ```

Technical Layer: Interface Binding

The assignment of an interface (e.g., eth1) to a specific zone (internal-apps) tells the firewalld daemon to apply the rules of that zone to all packets entering or leaving that specific hardware interface. This is a powerful way to separate public-facing traffic (assigned to the public zone) from backend database traffic (assigned to the internal or internal-apps zone).

Manual Verification and Troubleshooting

After deploying an Ansible playbook to disable or configure firewalld, verification is mandatory to ensure the desired state has been reached.

Verification Commands

The following commands should be used to audit the system state:

  • To list all active zones and their associated rules: sudo firewall-cmd --list-all-zones
  • To show the default zone configuration: sudo firewall-cmd --list-all
  • To check a specific zone's configuration: sudo firewall-cmd --zone=internal-apps --list-all
  • To list all rich rules: sudo firewall-cmd --list-rich-rules
  • To verify if a specific port is open: sudo firewall-cmd --query-port=8080/tcp

Troubleshooting Service Failures

If the ansible.builtin.systemd module fails to stop firewalld, it is often due to the service being in a "failed" state or having a complex dependency chain. In such cases, the manual emergency fallback involves the following root-level commands:

  • Stop the service: systemctl stop firewalld
  • Disable the service: systemctl disable firewalld
  • Mask the service: systemctl mask firewalld

Conclusion: Architectural Analysis of Firewall Orchestration

The decision to disable firewalld via Ansible is rarely an isolated act; it is typically a component of a larger security posture shift. When analyzed from a DevOps perspective, the transition from a daemon-based firewall (firewalld) to a configuration-based filter (nftables) reduces the system's attack surface by removing a running process and its associated D-Bus vulnerabilities.

The use of Ansible for this process transforms a manual, error-prone task into a repeatable, audited workflow. By utilizing the ansible.builtin.systemd module with masked: yes, administrators ensure a definitive state of "off," which is critical in environments where security audits require proof that unauthorized services are not merely stopped, but incapable of running. Furthermore, the ability to toggle between granular rule management using ansible.posix.firewalld and total deactivation allows an organization to evolve its network security strategy—moving from simple zone-based filtering to complex, site-specific nftables rulesets—without incurring the risk of manual configuration drift.

Sources

  1. Rocky Linux Forums
  2. OneUptime Blog
  3. Red Hat Enterprise Linux 7 Security Guide

Related Posts