Recently, I was tasked to apply CIS Benchmarks to some of my systems. Fortunately, Red Hat has some Ansible roles that make this process almost trivial.
[ Download now: A system administrator's guide to IT automation. ]
One challenge I encountered was with firewalld. One of the controls in the official CIS Ansible roles ensures that firewalld is enabled and running. However, some of my systems didn't have firewalld enabled, by design. This would mean proceeding to deploy the playbooks, which would cause applications to be inaccessible. That would be a huge cost to the organization.
After some research, I found the community.general.listen_ports_facts Ansible module, which made it possible to find all listening TCP and UDP ports. This module is based on the traditional netstat
command, which is part of the net-tools
package, and the newer ss
command. Because I wasn't sure which systems did not have net-tools
, I made sure net-tools
was installed as part of the process.
My solution
My solution, which is in my Git repository, contains four tasks:
- Install or upgrade the
net-tools
package. - Gather facts on listening ports by loading the
listen_ports_facts
module. - Add TCP ports listed by the
listen_ports_facts
to firewalld. - Display the ports added to the server.
Also, the comunity.general collection
needs to be installed. If it is not installed, you can install it with:
ansible-galaxy collection install community.general
Here's the playbook:
---
- name: Add Listening TCP port to Firewalld
hosts: localhost
gather_facts: true
become: true
tasks:
- name: Install or upgrade the net-tools package
yum:
name: net-tools
state: latest
- name: tcp ports facts
listen_ports_facts:
- name: Add ports to firewall
firewalld:
port: "{{ item }}/tcp"
state: enabled
immediate: true
permanent: true
loop: "{{ ansible_facts.tcp_listen | map(attribute='port') | sort | list }}"
register: ports_added_to_firewalld
- name: print Ports Added
debug:
var: ports_added_to_firewalld
I added this playbook to the role, and it solved my issue.
Future improvements
One issue I've discovered is the inability to filter specific ports on each server. Any ideas are welcome as either issues or pull requests to my Git repository. I did have to remove unwanted ports from a few specific servers but, all in all, this made my process much easier!
Über den Autor
I work as Unix/Linux Administrator with a passion for high availability systems and clusters. I am a student of performance and optimization of systems and DevOps. I have passion for anything IT related and most importantly automation, high availability, and security.
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Original Shows
Interessantes von den Experten, die die Technologien in Unternehmen mitgestalten
Produkte
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud-Services
- Alle Produkte anzeigen
Tools
- Training & Zertifizierung
- Eigenes Konto
- Kundensupport
- Für Entwickler
- Partner finden
- Red Hat Ecosystem Catalog
- Mehrwert von Red Hat berechnen
- Dokumentation
Testen, kaufen und verkaufen
Kommunizieren
Über Red Hat
Als weltweit größter Anbieter von Open-Source-Software-Lösungen für Unternehmen stellen wir Linux-, Cloud-, Container- und Kubernetes-Technologien bereit. Wir bieten robuste Lösungen, die es Unternehmen erleichtern, plattform- und umgebungsübergreifend zu arbeiten – vom Rechenzentrum bis zum Netzwerkrand.
Wählen Sie eine Sprache
Red Hat legal and privacy links
- Über Red Hat
- Jobs bei Red Hat
- Veranstaltungen
- Standorte
- Red Hat kontaktieren
- Red Hat Blog
- Diversität, Gleichberechtigung und Inklusion
- Cool Stuff Store
- Red Hat Summit