firewalld.yml 1.9 KB

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