iptables.yml 1.8 KB

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