This Task aims to automate the setup and management of firewall rules using Ansible. The automation will configure either UFW (Uncomplicated Firewall) or firewalld based on the system's firewall service, ensuring security best practices by allowing/denying specific ports and IPs.
[firewall_servers]
server1 ansible_host=192.168.1.100 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
server2 ansible_host=192.168.1.101 ansible_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
The playbook will:
Playbook: firewall_setup.yml
---
- name: Configure Firewall
hosts: firewall_servers
become: yes
tasks:
- name: Install UFW on Debian-based systems
apt:
name: ufw
state: present
when: ansible_os_family == "Debian"
- name: Install Firewalld on RHEL-based systems
yum:
name: firewalld
state: present
when: ansible_os_family == "RedHat"
- name: Start and enable firewall service (UFW)
service:
name: ufw
state: started
enabled: yes
when: ansible_os_family == "Debian"
- name: Start and enable firewall service (Firewalld)
service:
name: firewalld
state: started
enabled: yes
when: ansible_os_family == "RedHat"
- name: Set default policies (UFW)
command: ufw default deny incoming
when: ansible_os_family == "Debian"
- name: Set default policies (Firewalld)
command: firewall-cmd --set-default-zone=drop
when: ansible_os_family == "RedHat"
- name: Allow SSH (UFW)
command: ufw allow ssh
when: ansible_os_family == "Debian"
- name: Allow SSH (Firewalld)
command: firewall-cmd --permanent --add-service=ssh
when: ansible_os_family == "RedHat"
- name: Allow HTTP (UFW)
command: ufw allow 80/tcp
when: ansible_os_family == "Debian"
- name: Allow HTTP (Firewalld)
command: firewall-cmd --permanent --add-port=80/tcp
when: ansible_os_family == "RedHat"
- name: Allow HTTPS (UFW)
command: ufw allow 443/tcp
when: ansible_os_family == "Debian"
- name: Allow HTTPS (Firewalld)
command: firewall-cmd --permanent --add-port=443/tcp
when: ansible_os_family == "RedHat"
- name: Allow specific IP to access port 8080 (UFW)
command: ufw allow from 192.168.1.50 to any port 8080
when: ansible_os_family == "Debian"
- name: Allow specific IP to access port 8080 (Firewalld)
command: firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.50" port port="8080" protocol="tcp" accept'
when: ansible_os_family == "RedHat"
- name: Enable UFW
command: ufw enable
when: ansible_os_family == "Debian"
- name: Reload Firewalld
command: firewall-cmd --reload
when: ansible_os_family == "RedHat"
- name: Display Firewall Rules (UFW)
command: ufw status verbose
register: ufw_status
when: ansible_os_family == "Debian"
- name: Display Firewall Rules (Firewalld)
command: firewall-cmd --list-all
register: firewalld_status
when: ansible_os_family == "RedHat"
- debug:
msg: "{{ ufw_status.stdout }}"
when: ansible_os_family == "Debian"
- debug:
msg: "{{ firewalld_status.stdout }}"
when: ansible_os_family == "RedHat"
Run the playbook to apply firewall rules across all target servers.
ansible-playbook -i inventory firewall_setup.yml
sudo ufw status verbose
sudo firewall-cmd --list-all
sudo journalctl -xe | grep 'firewalld'
sudo dmesg | grep 'UFW'
If a misconfiguration occurs, revert firewall settings.
---
- name: Rollback Firewall Configuration
hosts: firewall_servers
become: yes
tasks:
- name: Reset UFW
command: ufw reset
when: ansible_os_family == "Debian"
- name: Disable Firewalld
command: systemctl stop firewalld
when: ansible_os_family == "RedHat"
ansible-playbook -i inventory firewall_rollback.yml
This project provides an automated, scalable, and secure solution for firewall management using Ansible. It ensures that firewall rules are consistently applied across multiple servers while offering logging, monitoring, and rollback mechanisms.