Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Created September 1, 2023 15:43
Show Gist options
  • Save dmccuk/b9173065adbaf44ac69fac1e404d08ff to your computer and use it in GitHub Desktop.
Save dmccuk/b9173065adbaf44ac69fac1e404d08ff to your computer and use it in GitHub Desktop.
---
- name: Manage iptables on RHEL6 or OEL6
hosts: your_target_hosts # You need to replace this with your actual target host group or host.
become: true
gather_facts: true # This is required to collect facts about the target host.
tasks:
- name: Check if the system is RHEL6 or OEL6
assert:
that:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version == '6'
msg: "This playbook is only intended for RHEL6 or OEL6."
- name: Check if iptables service is running
command: service iptables status
register: iptables_status
failed_when: false
changed_when: false
- name: Stop playbook if iptables is running
fail:
msg: "iptables is already running. Check manually."
when: "'running' in iptables_status.stdout"
- name: Copy iptables template
copy:
src: path_to_your_iptables_template_file # You need to replace this with your actual template file path.
dest: /etc/sysconfig/iptables
when: "'stopped' in iptables_status.stdout"
# Validation of the rules can be complex and might need custom scripting or checks.
# Due to the complexities involved, I'll skip that step here, but it's definitely something to consider in a production playbook.
- name: Start iptables service
service:
name: iptables
state: started
when: "'stopped' in iptables_status.stdout"
- name: Ensure iptables is on for runlevels 2,3,4
command: chkconfig --level 234 iptables on
when: "'stopped' in iptables_status.stdout"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment