install_zabbix-agent.playbook-condensed.yaml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. ---
  2. - name: "Install Zabbix agent using yum/dnf/apt"
  3. hosts: all
  4. gather_facts: True
  5. become: true
  6. # Put the IPs/hostnames of Zabbix servers and proxies here:
  7. vars:
  8. MONSRC:
  9. - ***CONTENTS REDACTED***
  10. MONSRCRANGE:
  11. - ***CONTENTS REDACTED***
  12. tasks:
  13. - name: "msg print to stdout: Debug ansible_facts"
  14. debug:
  15. msg:
  16. # - "{{ ansible_facts.service_mgr }}"
  17. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  18. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  19. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  20. #- "{{ ansible_facts.services_iptables'].state }}"
  21. #- ansible_facts.services "{{ ansible_facts.services }}"
  22. - name: Populate systemd service_facts
  23. service_facts:
  24. - debug:
  25. msg:
  26. - ansible_facts.services['firewalld.service'] "{{ ansible_facts.services['firewalld.service'] }}"
  27. when: "'firewalld.service' in services"
  28. - debug:
  29. msg:
  30. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  31. when: "'iptables.service' in services"
  32. - debug:
  33. msg:
  34. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  35. when: "'ufw.service' in services"
  36. - name: Show MONSRC variables
  37. debug:
  38. msg: "MONSRCs: {{ MONSRC[0] }}"
  39. - name: Gather package facts
  40. package_facts:
  41. manager: auto
  42. - debug:
  43. msg:
  44. - ansible_all_ipv4_addresses "{{ ansible_all_ipv4_addresses}}"
  45. # - name: "Is firewalld.service or ufw.service enabled?"
  46. # debug:
  47. # msg:
  48. # - "{{ ansible_facts.services['firewalld.service'].status }}"
  49. # - "{{ ansible_facts.services['ufw.service'].status }}"
  50. - name: Install Zabbix repo GPG key for ALL RHEL like
  51. rpm_key:
  52. state: present
  53. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-08EFA7DD
  54. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  55. #06-05-2024: added ignore_errors. Getting "Hash algorithm SHA1 not available." Not distro/version specific error.
  56. - name: Install OLD Zabbix repo GPG key for ALL RHEL like
  57. rpm_key:
  58. state: present
  59. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-A14FE591
  60. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  61. ignore_errors: true
  62. #RHEL-6 Like:
  63. - name: Install Zabbix rpm key if distro RHEL-6 like
  64. rpm_key:
  65. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-08EFA7DD
  66. state: present
  67. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  68. - name: Install zabbix-agent2 from specified rpm if distro is RH6-like
  69. yum:
  70. name: https://repo.zabbix.com/zabbix/6.4/rhel/6/x86_64/zabbix-agent2-6.4.8-release2.el6.x86_64.rpm
  71. state: installed
  72. when: (ansible_facts['distribution_file_variety'] == 'RedHat') and (ansible_facts['distribution_major_version'] == '6')
  73. #RHEL-7 like:
  74. - name: Install Official Zabbix repo for RHEL-7 like, incl. Fedora 19 - 27
  75. yum_repository:
  76. name: zabbix
  77. description: Install Zabbix official repo for RHEL-7 like
  78. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/$releasever/x86_64/
  79. enabled: yes
  80. when:
  81. - ansible_facts['distribution_file_variety'] == 'RedHat'
  82. - ansible_facts['distribution_major_version'] == '7' or ansible_facts['distribution_major_version'] == '19' or ansible_facts['distribution_major_version'] == '20' or ansible_facts['distribution_major_version'] == '21' or ansible_facts['distribution_major_version'] == '22' or ansible_facts['distribution_major_version'] == '23' or ansible_facts['distribution_major_version'] == '24' or ansible_facts['distribution_major_version'] == '25' or ansible_facts['distribution_major_version'] == '26' or ansible_facts['distribution_major_version'] == '27'
  83. - name: yum-clean-metadata for RHEL-7 like, incl. Fedora 19 - 27
  84. ansible.builtin.command: /usr/bin/yum clean metadata
  85. when:
  86. - ansible_facts['distribution_file_variety'] == 'RedHat'
  87. - ansible_facts['distribution_major_version'] == '7' or ansible_facts['distribution_major_version'] == '19' or ansible_facts['distribution_major_version'] == '20' or ansible_facts['distribution_major_version'] == '21' or ansible_facts['distribution_major_version'] == '22' or ansible_facts['distribution_major_version'] == '23' or ansible_facts['distribution_major_version'] == '24' or ansible_facts['distribution_major_version'] == '25' or ansible_facts['distribution_major_version'] == '26' or ansible_facts['distribution_major_version'] == '27'
  88. - name: Install zabbix-agent2 for RHEL-7 like
  89. yum:
  90. name:
  91. - zabbix-agent2
  92. disablerepo: "epel"
  93. state: present
  94. when:
  95. - ansible_facts['distribution_file_variety'] == 'RedHat'
  96. - ansible_facts['distribution_major_version'] == '7' or ansible_facts['distribution_major_version'] == '19' or ansible_facts['distribution_major_version'] == '20' or ansible_facts['distribution_major_version'] == '21' or ansible_facts['distribution_major_version'] == '22' or ansible_facts['distribution_major_version'] == '23' or ansible_facts['distribution_major_version'] == '24' or ansible_facts['distribution_major_version'] == '25' or ansible_facts['distribution_major_version'] == '26' or ansible_facts['distribution_major_version'] == '27'
  97. #RHEL-8 like:
  98. - name: Install Official Zabbix repo for RHEL-8 like incl. Fedora 28 - 33
  99. yum_repository:
  100. name: zabbix
  101. description: Install Zabbix official repo for RHEL-8 like incl. Fedora 28 - 33
  102. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/
  103. enabled: yes
  104. when:
  105. - ansible_facts['distribution_file_variety'] == 'RedHat'
  106. - ansible_facts['distribution_major_version'] == '8' or ansible_facts['distribution_major_version'] == '28' or ansible_facts['distribution_major_version'] == '29' or ansible_facts['distribution_major_version'] == '30' or ansible_facts['distribution_major_version'] == '31' or ansible_facts['distribution_major_version'] == '32' or ansible_facts['distribution_major_version'] == '33'
  107. - name: yum-clean-metadata for RHEL-8 like incl. Fedora 28 - 33
  108. ansible.builtin.command: /usr/bin/yum clean metadata
  109. when:
  110. - ansible_facts['distribution_file_variety'] == 'RedHat'
  111. - ansible_facts['distribution_major_version'] == '8' or ansible_facts['distribution_major_version'] == '28' or ansible_facts['distribution_major_version'] == '29' or ansible_facts['distribution_major_version'] == '30' or ansible_facts['distribution_major_version'] == '31' or ansible_facts['distribution_major_version'] == '32' or ansible_facts['distribution_major_version'] == '33'
  112. #RHEL-9 like:
  113. - name: Install Official Zabbix repo for RHEL-9 like incl. Fedora 34 - 40
  114. yum_repository:
  115. name: zabbix
  116. description: Install Zabbix official repo for RHEL-9 like incl. Fedora 34 - 40
  117. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/
  118. enabled: yes
  119. when:
  120. - ansible_facts['distribution_file_variety'] == 'RedHat'
  121. - ansible_facts['distribution_major_version'] == '9' or ansible_facts['distribution_major_version'] == '34' or ansible_facts['distribution_major_version'] == '35' or ansible_facts['distribution_major_version'] == '36' or ansible_facts['distribution_major_version'] == '37' or ansible_facts['distribution_major_version'] == '38' or ansible_facts['distribution_major_version'] == '39' or ansible_facts['distribution_major_version'] == '40'
  122. - name: Install Zabbix repo GPG key for ALL RHEL like
  123. rpm_key:
  124. state: present
  125. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-08EFA7DD
  126. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  127. - name: yum-clean-metadata for RHEL-9 like, incl. Fedora 34-40
  128. ansible.builtin.command: /usr/bin/yum clean metadata
  129. when:
  130. - ansible_facts['distribution_file_variety'] == 'RedHat'
  131. - ansible_facts['distribution_major_version'] == '9' or ansible_facts['distribution_major_version'] == '34' or ansible_facts['distribution_major_version'] == '35' or ansible_facts['distribution_major_version'] == '36' or ansible_facts['distribution_major_version'] == '37' or ansible_facts['distribution_major_version'] == '38' or ansible_facts['distribution_major_version'] == '39' or ansible_facts['distribution_major_version'] == '40'
  132. - name: Install zabbix-agent2 for RHEL-9 like, incl. Fedora 34-40
  133. yum:
  134. name:
  135. - zabbix-agent2
  136. disablerepo: "epel"
  137. state: present
  138. disable_gpg_check: true
  139. when:
  140. - ansible_facts['distribution_file_variety'] == 'RedHat'
  141. - ansible_facts['distribution_major_version'] == '9' or ansible_facts['distribution_major_version'] == '34' or ansible_facts['distribution_major_version'] == '35' or ansible_facts['distribution_major_version'] == '36' or ansible_facts['distribution_major_version'] == '37' or ansible_facts['distribution_major_version'] == '38' or ansible_facts['distribution_major_version'] == '39' or ansible_facts['distribution_major_version'] == '40'
  142. ############################################### Debian section #################################################################################################
  143. - name: Install zabbix-agent2 from specified deb file if distro is Deb12, "Bookworm"
  144. apt:
  145. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian12_amd64.deb
  146. state: present
  147. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '12')
  148. - name: Install zabbix-agent2 from specified deb file if distro is Deb11, "Bullseye"
  149. apt:
  150. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian11_amd64.deb
  151. state: present
  152. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '11')
  153. - name: Install zabbix-agent2 from specified deb file if distro is Deb10, "Buster"
  154. apt:
  155. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian10_amd64.deb
  156. state: present
  157. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '10')
  158. - name: Install zabbix-agent2 from specified deb file if distro is Deb9, "Stretch"
  159. apt:
  160. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian9_amd64.deb
  161. state: present
  162. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '9')
  163. ############################################## Begin Ubuntu section ###################################################
  164. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 22.xx, "Jammy Jellyfish"
  165. apt:
  166. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu22.04_amd64.deb
  167. state: present
  168. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '22')
  169. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 20.xx, "Focal Fossa"
  170. apt:
  171. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu20.04_amd64.deb
  172. state: present
  173. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '20')
  174. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 18.xx, "Bionic Bever"
  175. apt:
  176. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu18.04_amd64.deb
  177. state: present
  178. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '18')
  179. - name: Create /etc/zabbix w/ appropriate permissinos
  180. file:
  181. path: /etc/zabbix
  182. owner: zabbix
  183. group: root
  184. mode: '0755'
  185. state: directory
  186. # Copy Zabbix agent config file to host
  187. - name: use stat module to determine if /etc/zabbix/zabbix_agent2.conf already exists on the hosts
  188. stat:
  189. path: "/etc/zabbix/zabbix_agent2.conf"
  190. register: result
  191. - name: perform copy of zabbix agent config if it doesn't already exist
  192. copy:
  193. src: include/zabbix_agent2.conf
  194. dest: /etc/zabbix/zabbix_agent2.conf
  195. owner: zabbix
  196. group: root
  197. mode: '0640'
  198. when: not result.stat.exists
  199. - name: Show result of stat
  200. debug:
  201. msg: "/etc/zabbix/zabbix_agent2.conf DOES NOT exist!"
  202. when: not result.stat.exists
  203. - name: Show result
  204. debug:
  205. msg: "/etc/zabbix/zabbix_agent2.conf exists!"
  206. when: result.stat.exists
  207. # Create /etc/zabbix w/ appropriate permissinos
  208. - name: Create /etc/zabbix/certs w/ appropriate permissions
  209. file:
  210. path: /etc/zabbix/certs
  211. owner: zabbix
  212. group: root
  213. mode: '0750'
  214. state: directory
  215. # Copy Zabbix ca.cert to host
  216. - name: perform copy ca.cert
  217. copy:
  218. src: include/certs/ca.cert
  219. dest: /etc/zabbix/certs/ca.cert
  220. owner: zabbix
  221. group: root
  222. mode: '0640'
  223. # Copy Zabbix client.cert to host
  224. - name: perform copy client.cert
  225. copy:
  226. src: include/certs/client.cert
  227. dest: /etc/zabbix/certs/client.cert
  228. owner: zabbix
  229. group: root
  230. mode: '0640'
  231. # Copy Zabbix client_private.key to host
  232. - name: perform copy ca.cert
  233. copy:
  234. src: include/certs/client_private.key
  235. dest: /etc/zabbix/certs/client_private.key
  236. owner: zabbix
  237. group: root
  238. mode: '0640'
  239. - name : remove /var/log/zabbix/zabbix_agent2.log file if present
  240. file:
  241. path: /var/log/zabbix/zabbix_agent2.log
  242. state: absent
  243. # Copy Zabbix agent PSK
  244. - name: perform copy of Zabbix agent PSK
  245. copy:
  246. src: include/psk.key
  247. dest: /etc/zabbix/psk.key
  248. owner: zabbix
  249. group: root
  250. mode: '0440'
  251. # Ensure /var/log/zabbix/ exists w/ correct permissions
  252. - name: Create /var/log/zabbix if needed
  253. file:
  254. path: /var/log/zabbix
  255. state: directory
  256. owner: zabbix
  257. group: zabbix
  258. mode: '0775'
  259. # Remove /var/log/zabbix/zabbix_agent2.log doesn't exist
  260. - name: rm /var/log/zabbix/zabbix_agent2.log
  261. file:
  262. path: /var/log/zabbix/zabbix_agent2.log
  263. state: absent
  264. ############## Add special permissions for the zabbix user to collect certain data from soures like the dmidecode program ######### (Addded 05-13-2024)
  265. - name: Allow the zabbix user to run sudo dmidecode w/o a nopassword
  266. sudoers:
  267. name: zabbix_dmidecode
  268. state: present
  269. user: zabbix
  270. commands:
  271. - /usr/sbin/dmidecode
  272. nopassword: true
  273. ############################################################################################################################################
  274. ############################################################################################################################################
  275. ######### Firewall stuff ###################################################################################################################
  276. ########## firewalld section ################################################################################################################
  277. #################################
  278. # Determine and set the firewall method
  279. - name: Set variable to indicate which firewall method is being used by a systemd
  280. set_fact:
  281. FW_METHOD: "ufw"
  282. when: "'ufw' in ansible_facts.packages"
  283. - name: Set variable to indicate which firewall method is being used by a systemd
  284. set_fact:
  285. FW_METHOD: "iptables"
  286. when: "'iptables' in ansible_facts.packages"
  287. - name: Set variable to indicate which firewall method is being used by a systemd
  288. set_fact:
  289. FW_METHOD: "firewalld"
  290. when:
  291. - ansible_facts.distribution_file_variety != "Debian"
  292. - ansible_facts.services['firewalld.service']['status'] == 'enabled' or ansible_facts.services['firewalld.service']['status'] == 'running'
  293. - name: Show value of FW_METHOD
  294. debug:
  295. msg: FW_METHOD is "{{ FW_METHOD }}"
  296. # Allow connections to :10050 on systems using firewalld:
  297. - name: allow :10050-10051/tcp incoming from $MONSRC0 ("{{ MONSRC[0] }}") using firewalld
  298. firewalld:
  299. port: 10050-10051/tcp
  300. permanent: True
  301. state: enabled
  302. immediate: True
  303. when: FW_METHOD is "firewalld"
  304. - name: allow :10050-10051/tcp incoming from $MONSRC1 ("{{ MONSRC[1] }}") using firewalld
  305. firewalld:
  306. port: 10050-10051/tcp
  307. permanent: True
  308. state: enabled
  309. immediate: True
  310. when: FW_METHOD is "firewalld"
  311. - name: allow :10050-10051/tcp incoming from $MONSRC2 ("{{ MONSRC[2] }}") using firewalld
  312. firewalld:
  313. port: 10050-10051/tcp
  314. permanent: True
  315. state: enabled
  316. immediate: True
  317. when:
  318. - FW_METHOD is "firewalld"
  319. - ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***")
  320. ########## iptables section #################################################################################################################
  321. - name: Open 10050/tcp from $MONSRC0 ("{{ MONSRC[0] }}") if iptables.service is enabled
  322. iptables:
  323. action: insert
  324. chain: INPUT
  325. source: "{{ MONSRC[0] }}"
  326. protocol: tcp
  327. destination_port: 10050:10051
  328. state: present
  329. jump: ACCEPT
  330. when: FW_METHOD is "iptables"
  331. - name: Open 10050/tcp from $MONSRC1 ("{{ MONSRC[1] }}") if iptables.service is enabled
  332. iptables:
  333. action: insert
  334. chain: INPUT
  335. source: "{{ MONSRC[1] }}"
  336. protocol: tcp
  337. destination_port: 10050:10051
  338. state: present
  339. jump: ACCEPT
  340. when: FW_METHOD is "iptables"
  341. - name: Open 10050/tcp from $MONSRC2 ("{{ MONSRC[2] }}") if iptables.service is enabled AND IP contains ***CONTENTS REDACTED***
  342. iptables:
  343. action: insert
  344. chain: INPUT
  345. source: "{{ MONSRC[2] }}"
  346. protocol: tcp
  347. destination_port: 10050:10051
  348. state: present
  349. jump: ACCEPT
  350. when:
  351. - FW_METHOD is "iptables"
  352. - ansible_all_ipv4_addresses is search("***CONTENTS REDACTED***")
  353. - name: Open 10050/tcp from $MONSRCRANGE0 ("{{ MONSRCRANGE[0] }}") if iptables.service is enabled
  354. iptables:
  355. action: insert
  356. chain: INPUT
  357. src_range: "{{ MONSRCRANGE[0] }}"
  358. protocol: tcp
  359. destination_port: 10050:10051
  360. state: present
  361. jump: ACCEPT
  362. when: FW_METHOD is "iptables"
  363. - name: Open 10050/tcp from $MONSRCRANGE1 ("{{ MONSRCRANGE[1] }}") if iptables.service is enabled
  364. iptables:
  365. action: insert
  366. chain: INPUT
  367. src_range: "{{ MONSRCRANGE[1] }}"
  368. protocol: tcp
  369. destination_port: 10050:10051
  370. state: present
  371. jump: ACCEPT
  372. when: FW_METHOD is "iptables"
  373. - name: Save current state of the firewall in system file if iptables is enabled
  374. iptables_state:
  375. state: saved
  376. path: /etc/sysconfig/iptables
  377. when: FW_METHOD is "iptables"
  378. ########## ufw section ######################################################################################################################
  379. # Allow connections to :10050 on systems using UFW:
  380. - name: allow :10050/tcp incoming, ufw
  381. ufw:
  382. rule: allow
  383. port: '10050'
  384. proto: tcp
  385. comment: Zabbix agent on 10050
  386. when:
  387. - FW_METHOD is "ufw"
  388. ######### End of firewall stuff ############################################################################################################
  389. ############################################################################################################################################
  390. ############################################################################################################################################
  391. # Enable zabbix-agent on systemd-enabled systems:
  392. - name: enable zabbix-agent2 service
  393. systemd:
  394. name: zabbix-agent2
  395. enabled: True
  396. masked: no
  397. state: started
  398. ignore_errors: False
  399. when: ansible_facts.service_mgr == "systemd"
  400. # Restart systemd service
  401. - name: restart zabbix-agent2.service, systemd
  402. systemd:
  403. name: zabbix-agent2
  404. enabled: True
  405. masked: no
  406. state: restarted
  407. ignore_errors: False
  408. when: ansible_facts.service_mgr == "systemd"