firewalld.yml 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ---
  2. - name: Install firewalld packages
  3. action: "{{ ansible_pkg_mgr }} name=firewalld state=present"
  4. register: install_result
  5. - name: Check if iptables-services is installed
  6. command: rpm -q iptables-services
  7. register: pkg_check
  8. failed_when: pkg_check.rc > 1
  9. changed_when: no
  10. - name: Ensure iptables services are not enabled
  11. service:
  12. name: "{{ item }}"
  13. state: stopped
  14. enabled: no
  15. with_items:
  16. - iptables
  17. - ip6tables
  18. when: pkg_check.rc == 0
  19. - name: Reload systemd units
  20. command: systemctl daemon-reload
  21. when: install_result | changed
  22. - name: Start and enable firewalld service
  23. service:
  24. name: firewalld
  25. state: started
  26. enabled: yes
  27. register: result
  28. - name: need to pause here, otherwise the firewalld service starting can sometimes cause ssh to fail
  29. pause: seconds=10
  30. when: result | changed
  31. - name: Mask iptables services
  32. command: systemctl mask "{{ item }}"
  33. register: result
  34. changed_when: "'iptables' in result.stdout"
  35. with_items:
  36. - iptables
  37. - ip6tables
  38. when: pkg_check.rc == 0
  39. ignore_errors: yes
  40. # TODO: Ansible 1.9 will eliminate the need for separate firewalld tasks for
  41. # enabling rules and making them permanent with the immediate flag
  42. - name: Add firewalld allow rules
  43. firewalld:
  44. port: "{{ item.port }}"
  45. permanent: false
  46. state: enabled
  47. with_items: os_firewall_allow
  48. when: os_firewall_allow is defined
  49. - name: Persist firewalld allow rules
  50. firewalld:
  51. port: "{{ item.port }}"
  52. permanent: true
  53. state: enabled
  54. with_items: os_firewall_allow
  55. when: os_firewall_allow is defined
  56. - name: Remove firewalld allow rules
  57. firewalld:
  58. port: "{{ item.port }}"
  59. permanent: false
  60. state: disabled
  61. with_items: os_firewall_deny
  62. when: os_firewall_deny is defined
  63. - name: Persist removal of firewalld allow rules
  64. firewalld:
  65. port: "{{ item.port }}"
  66. permanent: true
  67. state: disabled
  68. with_items: os_firewall_deny
  69. when: os_firewall_deny is defined