update_firewall_rules.yaml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ---
  2. - name: "Update firewall rules for Zabbix agent"
  3. hosts: all
  4. gather_facts: True
  5. become: true
  6. # Put the IPs/hostnames of Zabbix servers and proxies here:
  7. vars:
  8. OLDMONSRC:
  9. - ***CONTENTS REDACTED***
  10. - ***CONTENTS REDACTED***
  11. MONSRC:
  12. - ***CONTENTS REDACTED***
  13. OLDMONSRCRANGE:
  14. - ***CONTENTS REDACTED***
  15. MONSRCRANGE:
  16. - ***CONTENTS REDACTED***
  17. tasks:
  18. - name: "msg print to stdout: Debug ansible_facts"
  19. debug:
  20. msg:
  21. # - "{{ ansible_facts.service_mgr }}"
  22. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  23. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  24. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  25. #- "{{ ansible_facts.services_iptables'].state }}"
  26. #- ansible_facts.services "{{ ansible_facts.services }}"
  27. - name: Gather package facts
  28. package_facts:
  29. manager: auto
  30. - name: Populate systemd service_facts
  31. service_facts:
  32. - name: Show MONSRC variables
  33. debug:
  34. msg: "MONSRCs: {{ MONSRC[0-1] }}"
  35. - debug:
  36. msg:
  37. - ansible_facts.services['firewalld.service'] "{{ ansible_facts.services['firewalld.service'] }}"
  38. when: "'firewalld.service' in services"
  39. - debug:
  40. msg:
  41. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  42. when: "'iptables.service' in services"
  43. - debug:
  44. msg:
  45. - ansible_all_ipv4_addresses "{{ ansible_all_ipv4_addresses}}"
  46. - name: Gather package facts
  47. package_facts:
  48. manager: auto
  49. - name: Populate systemd service_facts
  50. service_facts:
  51. - name: Set variable to indicate which firewall method is being used by a systemd
  52. set_fact:
  53. FW_METHOD: "ufw"
  54. when: "'ufw' in ansible_facts.packages"
  55. - name: Set variable to indicate which firewall method is being used by a systemd
  56. set_fact:
  57. FW_METHOD: "iptables"
  58. when: "'iptables' in ansible_facts.packages"
  59. - name: Set variable to indicate which firewall method is being used by a systemd
  60. set_fact:
  61. FW_METHOD: "firewalld"
  62. when:
  63. - ansible_facts.distribution_file_variety != "Debian"
  64. - ansible_facts.services['firewalld.service']['status'] == 'enabled' or ansible_facts.services['firewalld.service']['status'] == 'running'
  65. - name: Show value of FW_METHOD
  66. debug:
  67. msg: FW_METHOD is "{{ FW_METHOD }}"
  68. #################################
  69. # Determine and set the firewall method
  70. - name: Set variable to indicate which firewall method is being used by a systemd
  71. set_fact:
  72. FW_METHOD: "ufw"
  73. when: "'ufw' in ansible_facts.packages"
  74. - name: Set variable to indicate which firewall method is being used by a systemd
  75. set_fact:
  76. FW_METHOD: "firewalld"
  77. when:
  78. - ansible_facts.distribution_file_variety != "Debian"
  79. - ansible_facts.services['firewalld.service']['status'] == 'enabled' or ansible_facts.services['firewalld.service']['status'] == 'running'
  80. - name: Set variable to indicate which firewall method is being used by a systemd
  81. set_fact:
  82. FW_METHOD: "firewalld"
  83. when:
  84. - ansible_facts.distribution_file_variety != "Debian"
  85. - ansible_facts.services['iptables.service']['status'] == 'enabled' or ansible_facts.services['iptables.service']['status'] == 'running'
  86. - name: Set variable to indicate which firewall method is being used by a systemd
  87. set_fact:
  88. FW_METHOD: "iptables"
  89. when: "'iptables' in ansible_facts.packages"
  90. - name: Show value of FW_METHOD
  91. debug:
  92. msg: FW_METHOD is "{{ FW_METHOD }}"
  93. - name: allow :10050-10051/tcp incoming using firewalld
  94. firewalld:
  95. port: 10050-10051/tcp
  96. permanent: True
  97. state: enabled
  98. immediate: True
  99. when: FW_METHOD == "firewalld"
  100. ########## iptables section #################################################################################################################
  101. - name: Remove old $OLDMONSRC0 ("{{ OLDMONSRC[0] }}") if iptables.service is enabled
  102. iptables:
  103. action: insert
  104. chain: INPUT
  105. source: "{{ OLDMONSRC[0] }}"
  106. protocol: tcp
  107. destination_port: 10050:10051
  108. state: absent
  109. jump: ACCEPT
  110. when: FW_METHOD == "iptables"
  111. ignore_errors: true
  112. - name: Remove old $OLDMONSRC1 ("{{ OLDMONSRC[1] }}") if iptables.service is enabled
  113. iptables:
  114. action: insert
  115. chain: INPUT
  116. source: "{{ OLDMONSRC[0] }}"
  117. protocol: tcp
  118. destination_port: 10050:10051
  119. state: absent
  120. jump: ACCEPT
  121. when: FW_METHOD == "iptables"
  122. ignore_errors: true
  123. - name: Open 10050/tcp from $MONSRC0 ("{{ MONSRC[0] }}") if iptables.service is enabled
  124. iptables:
  125. action: insert
  126. chain: INPUT
  127. source: "{{ MONSRC[0] }}"
  128. protocol: tcp
  129. destination_port: 10050:10051
  130. state: present
  131. jump: ACCEPT
  132. when: FW_METHOD == "iptables"
  133. ignore_errors: true
  134. - name: Open 10050/tcp if iptables.service is enabled AND IP contains 10135
  135. iptables:
  136. action: insert
  137. chain: INPUT
  138. source: "{{ MONSRC[1] }}"
  139. protocol: tcp
  140. destination_port: 10050:10051
  141. state: present
  142. jump: ACCEPT
  143. when:
  144. - FW_METHOD == "iptables"
  145. - ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***")
  146. ignore_errors: true
  147. - name: Remove 10050/tcp from $OLDMONSRCRANGE0 ("{{ OLDMONSRCRANGE[0] }}") if iptables.service is enabled
  148. iptables:
  149. action: insert
  150. chain: INPUT
  151. src_range: "{{ OLDMONSRCRANGE[0] }}"
  152. protocol: tcp
  153. destination_port: 10050:10051
  154. state: absent
  155. jump: ACCEPT
  156. when: FW_METHOD == "iptables"
  157. ignore_errors: true
  158. - name: Open 10050/tcp from $MONSRCRANGE0 ("{{ MONSRCRANGE[0] }}") if iptables.service is enabled
  159. iptables:
  160. action: insert
  161. chain: INPUT
  162. src_range: "{{ MONSRCRANGE[0] }}"
  163. protocol: tcp
  164. destination_port: 10050:10051
  165. state: present
  166. jump: ACCEPT
  167. when: FW_METHOD == "iptables"
  168. ignore_errors: true
  169. - name: Open 10050/tcp from ***CONTENTS REDACTED*** if iptables.service is enabled and IP contains ***CONTENTS REDACTED***, ***CONTENTS REDACTED***, ***CONTENTS REDACTED***
  170. iptables:
  171. action: insert
  172. chain: INPUT
  173. src_range: "***CONTENTS REDACTED***"
  174. protocol: tcp
  175. destination_port: 10050:10051
  176. state: present
  177. jump: ACCEPT
  178. when:
  179. - FW_METHOD == "iptables"
  180. - ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***") or ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***") or ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***")
  181. ignore_errors: true
  182. - name: Save current state of the firewall in system file if iptables is enabled
  183. iptables_state:
  184. state: saved
  185. path: /etc/sysconfig/iptables
  186. when: FW_METHOD == "iptables"
  187. ignore_errors: true
  188. ########## ufw section ######################################################################################################################
  189. # Allow connections to :10050 on systems using UFW:
  190. - name: allow :10050/tcp incoming, ufw
  191. ufw:
  192. rule: allow
  193. port: '10050'
  194. proto: tcp
  195. comment: Zabbix agent on 10050
  196. when: FW_METHOD == "ufw"
  197. ignore_errors: true
  198. ######### End of firewall stuff ############################################################################################################
  199. ############################################################################################################################################
  200. ############################################################################################################################################