iptables.yml 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ---
  2. - name: Install iptables packages
  3. yum:
  4. name: "{{ item }}"
  5. state: present
  6. with_items:
  7. - iptables
  8. - iptables-services
  9. register: install_result
  10. - name: Check if firewalld is installed
  11. command: rpm -q firewalld
  12. register: pkg_check
  13. failed_when: pkg_check.rc > 1
  14. changed_when: no
  15. - name: Ensure firewalld service is not enabled
  16. service:
  17. name: firewalld
  18. state: stopped
  19. enabled: no
  20. when: pkg_check.rc == 0
  21. - name: Reload systemd units
  22. command: systemctl daemon-reload
  23. when: install_result | changed
  24. - name: Start and enable iptables service
  25. service:
  26. name: iptables
  27. state: started
  28. enabled: yes
  29. register: result
  30. - name: need to pause here, otherwise the iptables service starting can sometimes cause ssh to fail
  31. pause: seconds=10
  32. when: result | changed
  33. # TODO: submit PR upstream to add mask/unmask to service module
  34. - name: Mask firewalld service
  35. command: systemctl mask firewalld
  36. register: result
  37. changed_when: "'firewalld' in result.stdout"
  38. when: pkg_check.rc == 0
  39. ignore_errors: yes
  40. - name: Add iptables allow rules
  41. os_firewall_manage_iptables:
  42. name: "{{ item.service }}"
  43. action: add
  44. protocol: "{{ item.port.split('/')[1] }}"
  45. port: "{{ item.port.split('/')[0] }}"
  46. with_items: os_firewall_allow
  47. when: os_firewall_allow is defined
  48. - name: Remove iptables rules
  49. os_firewall_manage_iptables:
  50. name: "{{ item.service }}"
  51. action: remove
  52. protocol: "{{ item.port.split('/')[1] }}"
  53. port: "{{ item.port.split('/')[0] }}"
  54. with_items: os_firewall_deny
  55. when: os_firewall_deny is defined