iptables.yml 1.7 KB

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