firewalld.yml 2.1 KB

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