firewalld.yml 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ignore_errors: yes
  42. # TODO: Ansible 1.9 will eliminate the need for separate firewalld tasks for
  43. # enabling rules and making them permanent with the immediate flag
  44. - name: Add firewalld allow rules
  45. firewalld:
  46. port: "{{ item.port }}"
  47. permanent: false
  48. state: enabled
  49. with_items: os_firewall_allow
  50. when: os_firewall_allow is defined
  51. - name: Persist firewalld allow rules
  52. firewalld:
  53. port: "{{ item.port }}"
  54. permanent: true
  55. state: enabled
  56. with_items: os_firewall_allow
  57. when: os_firewall_allow is defined
  58. - name: Remove firewalld allow rules
  59. firewalld:
  60. port: "{{ item.port }}"
  61. permanent: false
  62. state: disabled
  63. with_items: os_firewall_deny
  64. when: os_firewall_deny is defined
  65. - name: Persist removal of firewalld allow rules
  66. firewalld:
  67. port: "{{ item.port }}"
  68. permanent: true
  69. state: disabled
  70. with_items: os_firewall_deny
  71. when: os_firewall_deny is defined