install_rpm_or_deb.yaml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ---
  2. - name: "Install Zabbix agent using yum/dnf/apt"
  3. hosts: all
  4. gather_facts: True
  5. become: true
  6. tasks:
  7. - name: "msg print to stdout: Debug ansible_facts"
  8. debug:
  9. msg:
  10. - ansible_facts.distribution "{{ ansible_facts.distribution }}"
  11. - ansible_facts.distribution_major_version "{{ ansible_facts.distribution_major_version }}"
  12. - ansible_facts.distribution_file_variety "{{ ansible_facts.distribution_file_variety }}"
  13. - name: Populate systemd service_facts
  14. service_facts:
  15. - debug:
  16. msg:
  17. - ansible_facts.services['firewalld.service'] "{{ ansible_facts.services['firewalld.service'] }}"
  18. when: "'firewalld.service' in services"
  19. - debug:
  20. msg:
  21. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  22. when: "'iptables.service' in services"
  23. - debug:
  24. msg:
  25. - ansible_facts.services['iptables.service'] "{{ ansible_facts.services['iptables.service'] }}"
  26. when: "'ufw.service' in services"
  27. #RHEL-6 Like:
  28. - name: Install Zabbix rpm key if distro RHEL-6 like
  29. rpm_key:
  30. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-08EFA7DD
  31. state: present
  32. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  33. - name: Install zabbix-agent2 from specified rpm if distro is RH6-like
  34. yum:
  35. name: https://repo.zabbix.com/zabbix/6.4/rhel/6/x86_64/zabbix-agent2-6.4.8-release2.el6.x86_64.rpm
  36. state: installed
  37. when: (ansible_facts['distribution_file_variety'] == 'RedHat') and (ansible_facts['distribution_major_version'] == '6')
  38. #RHEL-7 like:
  39. - name: Install Official Zabbix repo for RHEL-7 like, incl. Fedora 19 - 27
  40. yum_repository:
  41. name: zabbix
  42. description: Install Zabbix official repo for RHEL-7 like
  43. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/$releasever/x86_64/
  44. enabled: yes
  45. when:
  46. - ansible_facts['distribution_file_variety'] == 'RedHat'
  47. - 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'
  48. - name: yum-clean-metadata for RHEL-7 like, incl. Fedora 19 - 27
  49. ansible.builtin.command: yum clean metadata
  50. when:
  51. - ansible_facts['distribution_file_variety'] == 'RedHat'
  52. - 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'
  53. - name: Install zabbix-agent2 + plugins for RHEL-7 like
  54. yum:
  55. name:
  56. - zabbix-agent2, zabbix-agent2-plugin-*
  57. disablerepo: "epel"
  58. state: present
  59. when:
  60. - ansible_facts['distribution_file_variety'] == 'RedHat'
  61. - 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'
  62. #RHEL-8 like:
  63. - name: Install Zabbix repo GPG key for ALL RHEL like, incl. Fedora 28 - 33
  64. rpm_key:
  65. state: present
  66. key: https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-08EFA7DD
  67. when: ansible_facts['distribution_file_variety'] == 'RedHat'
  68. - name: Install Official Zabbix repo for RHEL-8 like incl. Fedora 28 - 33
  69. yum_repository:
  70. name: zabbix
  71. description: Install Zabbix official repo for RHEL-8 like incl. Fedora 28 - 33
  72. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/
  73. enabled: yes
  74. when:
  75. - ansible_facts['distribution_file_variety'] == 'RedHat'
  76. - 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'
  77. - name: yum-clean-metadata for RHEL-8 like incl. Fedora 28 - 33
  78. ansible.builtin.command: yum clean metadata
  79. when:
  80. - ansible_facts['distribution_file_variety'] == 'RedHat'
  81. - 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'
  82. - name: Install Official Zabbix repo for RHEL-9 like, incl. Fedora 34-40
  83. yum_repository:
  84. name: zabbix
  85. description: Install Zabbix official repo for RHEL-9 like, incl. Fedora 34-40
  86. baseurl: https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/
  87. enabled: yes
  88. when:
  89. - ansible_facts['distribution_file_variety'] == 'RedHat'
  90. - 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'
  91. - name: yum-clean-metadata for RHEL-9 like, incl. Fedora 34-40
  92. ansible.builtin.command: yum clean metadata
  93. when:
  94. - ansible_facts['distribution_file_variety'] == 'RedHat'
  95. - 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'
  96. - name: Install zabbix-agent2 + plugins for RHEL-9 like, incl. Fedora 34-40
  97. yum:
  98. name:
  99. - zabbix-agent2, zabbix-agent2-plugin-*
  100. disablerepo: "epel"
  101. state: present
  102. disable_gpg_check: true
  103. when:
  104. - ansible_facts['distribution_file_variety'] == 'RedHat'
  105. - 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'
  106. ############################################### Debian section #################################################################################################
  107. - name: Install zabbix-agent2 from specified deb file if distro is Deb12, "Bookworm"
  108. apt:
  109. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian12_amd64.deb
  110. state: present
  111. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '12')
  112. - name: Install zabbix-agent2 from specified deb file if distro is Deb11, "Bullseye"
  113. apt:
  114. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian11_amd64.deb
  115. state: present
  116. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '11')
  117. - name: Install zabbix-agent2 from specified deb file if distro is Deb10, "Buster"
  118. apt:
  119. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian10_amd64.deb
  120. state: present
  121. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '10')
  122. - name: Install zabbix-agent2 from specified deb file if distro is Deb9, "Stretch"
  123. apt:
  124. deb: https://repo.zabbix.com/zabbix/6.4/debian/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bdebian9_amd64.deb
  125. state: present
  126. when: (ansible_facts['distribution'] == 'Debian') and (ansible_facts['distribution_major_version'] == '9')
  127. ############################################## Begin Ubuntu section ###################################################
  128. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 22.xx, "Jammy Jellyfish"
  129. apt:
  130. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu22.04_amd64.deb
  131. state: present
  132. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '22')
  133. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 20.xx, "Focal Fossa"
  134. apt:
  135. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu20.04_amd64.deb
  136. state: present
  137. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '20')
  138. - name: Install zabbix-agent2 from specified deb file if distro is Ubuntu 18.xx, "Bionic Bever"
  139. apt:
  140. deb: https://repo.zabbix.com/zabbix/6.4/ubuntu/pool/main/z/zabbix/zabbix-agent2_6.4.8-2%2Bubuntu18.04_amd64.deb
  141. state: present
  142. when: (ansible_facts['distribution'] == 'Ubuntu') and (ansible_facts['distribution_major_version'] == '18')
  143. - name: Create /etc/zabbix w/ appropriate permissinos
  144. file:
  145. path: /etc/zabbix
  146. owner: zabbix
  147. group: root
  148. mode: '0755'
  149. state: directory
  150. # Copy Zabbix agent config file to host
  151. - name: perform copy of zabbix agent config
  152. copy:
  153. src: ~/install_packages/zabbix/zabbix_agent2.conf
  154. dest: /etc/zabbix/zabbix_agent2.conf
  155. owner: zabbix
  156. group: root
  157. mode: '0640'
  158. # Create /etc/zabbix w/ appropriate permissinos
  159. - name: Create /etc/zabbix/certs w/ appropriate permissions
  160. file:
  161. path: /etc/zabbix/certs
  162. owner: zabbix
  163. group: root
  164. mode: '0750'
  165. state: directory
  166. # Copy Zabbix ca.cert to host
  167. - name: perform copy ca.cert
  168. copy:
  169. src: ~/install_packages/zabbix/certs/ca.cert
  170. dest: /etc/zabbix/certs/ca.cert
  171. owner: zabbix
  172. group: root
  173. mode: '0640'
  174. # Copy Zabbix client.cert to host
  175. - name: perform copy client.cert
  176. copy:
  177. src: ~/install_packages/zabbix/certs/client.cert
  178. dest: /etc/zabbix/certs/client.cert
  179. owner: zabbix
  180. group: root
  181. mode: '0640'
  182. # Copy Zabbix client_private.key to host
  183. - name: perform copy ca.cert
  184. copy:
  185. src: ~/install_packages/zabbix/certs/client_private.key
  186. dest: /etc/zabbix/certs/client_private.key
  187. owner: zabbix
  188. group: root
  189. mode: '0640'
  190. - name : remove /etc/zabbix_agentd.conf file if present
  191. file:
  192. path: /etc/zabbix_agentd.conf
  193. state: absent
  194. # Copy Zabbix agent PSK
  195. - name: perform copy of Zabbix agent PSK
  196. copy:
  197. src: ~/install_packages/zabbix/psk.key
  198. dest: /etc/zabbix/psk.key
  199. owner: zabbix
  200. group: root
  201. mode: '0440'
  202. # Ensure /var/log/zabbix/ exists w/ correct permissions
  203. - name: Create /var/log/zabbix if needed
  204. file:
  205. path: /var/log/zabbix
  206. state: directory
  207. owner: zabbix
  208. group: zabbix
  209. mode: '0775'
  210. # Ensure /var/log/zabbix/zabbix_agentd.log exists
  211. - name: Touch /var/log/zabbix/zabbix_agent2.log
  212. file:
  213. path: /var/log/zabbix/zabbix_agent2.log
  214. state: touch
  215. owner: zabbix
  216. group: zabbix
  217. mode: '0664'
  218. # Allow connections to :10050 on systems using firewalld:
  219. - name: allow :10050/tcp incoming, firewalld
  220. firewalld:
  221. port:
  222. - 10050/tcp
  223. - 10051/tcp
  224. permanent: True
  225. state: enabled
  226. immediate: True
  227. when: ansible_facts.services['firewalld.service']['status'] == 'enabled'
  228. ignore_errors: yes
  229. # Allow connections to :10050 on systems using iptables.service:
  230. - name: Open 10050/tcp if iptables.service is running
  231. iptables:
  232. chain: INPUT
  233. src_range: "***CONTENTS REDACTED***"
  234. protocol: tcp
  235. destination_ports:
  236. - "10050"
  237. - "10051"
  238. jump: ACCEPT
  239. when: ansible_facts.services['iptables.service']['status'] == 'enabled'
  240. ignore_errors: yes
  241. # Allow connections to :10050 on systems using UFW:
  242. - name: allow :10050/tcp incoming, ufw
  243. ufw:
  244. rule: allow
  245. port: '10050'
  246. proto: tcp
  247. comment: Zabbix agent on 10050
  248. when:
  249. - "'ufw' in services"
  250. - ansible_facts.services['ufw.service']['status'] == 'enabled'
  251. ignore_errors: yes
  252. # Enable zabbix-agent on systemd-enabled systems:
  253. - name: enable zabbix-agent2 service
  254. systemd:
  255. name: zabbix-agent2
  256. enabled: True
  257. masked: no
  258. state: started
  259. ignore_errors: False
  260. #when: ansible_facts.services['source'].source == systemd
  261. when: ansible_facts.service_mgr == "systemd"
  262. # Restart systemd service
  263. - name: restart zabbix-agent2.service, systemd
  264. systemd:
  265. name: zabbix-agent2
  266. enabled: True
  267. masked: no
  268. state: restarted
  269. ignore_errors: False
  270. when: ansible_facts.service_mgr == "systemd"