{"id":30881,"date":"2024-02-13T14:14:19","date_gmt":"2024-02-13T13:14:19","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=30881"},"modified":"2024-09-10T17:46:02","modified_gmt":"2024-09-10T15:46:02","slug":"configure-zabbix-with-ansible","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/","title":{"rendered":"Configure Zabbix with Ansible"},"content":{"rendered":"\n<p>Besides the ability to setup your Zabbix server, proxy and agents, the Ansible collection for Zabbix (ie. <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/community\/zabbix\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">community.zabbix<\/a>) also allows to configure every <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/community\/zabbix\/index.html#plugin-index\" target=\"_blank\" rel=\"noreferrer noopener\">aspects<\/a> of your Zabbix system. From hosts to users, discovery rules or even templates. In this blog post, we will see how this module helped me to <strong>automatically<\/strong> configure hosts I provision with YaK in my Zabbix server configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-setup-community-zabbix\">Setup community.zabbix<\/h2>\n\n\n\n<p>The first step is easy and fast. In your <a href=\"https:\/\/www.dbi-services.com\/products\/yak\/\" target=\"_blank\" rel=\"noreferrer noopener\">YaK<\/a> environment, run the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nansible-galaxy collection install community.zabbix\n<\/pre><\/div>\n\n\n<p>This collection is actually an interface with <a href=\"https:\/\/www.zabbix.com\/documentation\/current\/en\/manual\/api\" target=\"_blank\" rel=\"noreferrer noopener\">Zabbix API<\/a> which make our work easier when we already know Ansible and don&#8217;t want to go through complex HTTP API calls (see my other <a href=\"https:\/\/www.dbi-services.com\/blog\/zabbix-configuration-with-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">blog post<\/a> about it).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-setup-ansible-variables\">Setup Ansible Variables<\/h2>\n\n\n\n<p>If you know Ansible, you know that all tasks are going through a ssh connection to the target host. Once agent is installed on target host, we want to configure it in Zabbix server. This is achieved by modifying few variables before calling zabbix modules:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- name: Set api configuration facts\n  ansible.builtin.set_fact:\n    ansible_network_os: community.zabbix.zabbix\n    ansible_connection: httpapi\n    ansible_httpapi_port: 80\n    ansible_zabbix_url_path: &#039;&#039;\n    ansible_host: &quot;{{ zabbix_server_ip }}&quot; # Zabbix server\n    ansible_user: &quot;{{ zabbix_api_login_user }}&quot;\n<\/pre><\/div>\n\n\n<p>For requests to be redirected, all tasks concerning our host must go to the zabbix server (line 7). Zabbix server does not understand ssh connection, but <code>httpapi<\/code> connections (line 4) with <code>ansible_network_os <\/code>specific to the API (line 3). Authentication against the http api is made with <code>ansible_user<\/code> as login and <code>ansible_httpapi_pass<\/code> as password. For better security, we could create an API token and use https.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-usage\">Usage<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-host-group\">Host Group<\/h4>\n\n\n\n<p>Before creating our host, we can start with a simpler task like creating a host group:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- name: Create host groups\n  community.zabbix.zabbix_group:\n    host_group: &quot;{{ zabbix_agent_groups }}&quot;\n  register: zabbix_api_hostgroup_created\n  until: zabbix_api_hostgroup_created is succeeded\n<\/pre><\/div>\n\n\n<p>This task calls <code>zabbix_group<\/code> module with a list of group in <code>host_group<\/code> argument. Could it be more easier than that?<\/p>\n\n\n\n<p>Note that lines 4 and 5 are there to ensure host group is created as concurrent access (while running against multiple hosts) might raise creation failure. The default value for <code>retries<\/code> is 3 and with a delay of 5 seconds between each retry.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-host\">Host<\/h4>\n\n\n\n<p>To create a host in Zabbix configuration, there are a bit more variables to provide to the module:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n- name: Create host in zabbix server configuration\n  community.zabbix.zabbix_host:\n    host_name: &quot;{{ hostname }}&quot;\n    host_groups: &quot;{{ zabbix_agent_groups }}&quot;\n    link_templates: &quot;{{ zabbix_agent_link_templates }}&quot;\n    interfaces: &quot;{{ zabbix_agent_interfaces }}&quot;\n    tags: &quot;{{ zabbix_agent_tags }}&quot;\n    tls_connect: 2 # PSK\n    tls_psk_identity: &quot;{{ zabbix_agent2_tlspskidentity }}&quot;\n    tls_psk: &quot;{{ zabbix_agent2_tlspsk_secret }}&quot;\n  register: zabbix_api_host_created\n  until: zabbix_api_host_created is succeeded\n<\/pre><\/div>\n\n\n<p><code>host_name<\/code> is the how the host will be named in Zabbix configuration (coming from inventory &#8211; line 3). <code>host_groups<\/code> is the list of groups we&#8217;ve created previously. <code>link_templates<\/code> is a list of Zabbix templates.<\/p>\n\n\n\n<p><code>interfaces<\/code> (line 6) will be the list of defined interfaces to reach the Zabbix agent from the Zabbix server. This variable content looks like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n    zabbix_agent_interfaces:\n      - type: 1\n        main: 1\n        useip: &quot;{{ zabbix_useuip }}&quot;\n        ip: &quot;{{ ansible_host }}&quot;\n        dns: &quot;&quot;\n        port: &quot;{{ zabbix_agent_listenport }}&quot;\n<\/pre><\/div>\n\n\n<p>Next <code>tls_*<\/code> variables are set to encrypt trafic between agent and server.<\/p>\n\n\n\n<p><code>tags<\/code> variable allows to have host added with tags in Zabbix configuration.<\/p>\n\n\n\n<p>Et voil\u00e0!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"69\" src=\"http:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/02\/2024-02-13-14_03_25-Configuration-of-hosts-\u2014-Mozilla-Firefox-1024x69.png\" alt=\"\" class=\"wp-image-30907\" srcset=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/02\/2024-02-13-14_03_25-Configuration-of-hosts-\u2014-Mozilla-Firefox-1024x69.png 1024w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/02\/2024-02-13-14_03_25-Configuration-of-hosts-\u2014-Mozilla-Firefox-300x20.png 300w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/02\/2024-02-13-14_03_25-Configuration-of-hosts-\u2014-Mozilla-Firefox-768x52.png 768w, https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2024\/02\/2024-02-13-14_03_25-Configuration-of-hosts-\u2014-Mozilla-Firefox.png 1180w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Besides the ability to setup your Zabbix server, proxy and agents, the Ansible collection for Zabbix (ie. community.zabbix) also allows to configure every aspects of your Zabbix system. From hosts to users, discovery rules or even templates. In this blog post, we will see how this module helped me to automatically configure hosts I provision [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1321,1320],"tags":[2600,143,2430],"type_dbi":[],"class_list":["post-30881","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","tag-ansible-2","tag-monitoring","tag-zabbix"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Configure Zabbix with Ansible - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Configure Zabbix with Ansible\" \/>\n<meta property=\"og:description\" content=\"Besides the ability to setup your Zabbix server, proxy and agents, the Ansible collection for Zabbix (ie. community.zabbix) also allows to configure every aspects of your Zabbix system. From hosts to users, discovery rules or even templates. In this blog post, we will see how this module helped me to automatically configure hosts I provision [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-13T13:14:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-10T15:46:02+00:00\" \/>\n<meta name=\"author\" content=\"Middleware Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Middleware Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\"},\"author\":{\"name\":\"Middleware Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"headline\":\"Configure Zabbix with Ansible\",\"datePublished\":\"2024-02-13T13:14:19+00:00\",\"dateModified\":\"2024-09-10T15:46:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\"},\"wordCount\":399,\"commentCount\":2,\"keywords\":[\"Ansible\",\"Monitoring\",\"Zabbix\"],\"articleSection\":[\"Ansible\",\"DevOps\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\",\"name\":\"Configure Zabbix with Ansible - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2024-02-13T13:14:19+00:00\",\"dateModified\":\"2024-09-10T15:46:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Configure Zabbix with Ansible\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\",\"name\":\"Middleware Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"caption\":\"Middleware Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Configure Zabbix with Ansible - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/","og_locale":"en_US","og_type":"article","og_title":"Configure Zabbix with Ansible","og_description":"Besides the ability to setup your Zabbix server, proxy and agents, the Ansible collection for Zabbix (ie. community.zabbix) also allows to configure every aspects of your Zabbix system. From hosts to users, discovery rules or even templates. In this blog post, we will see how this module helped me to automatically configure hosts I provision [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/","og_site_name":"dbi Blog","article_published_time":"2024-02-13T13:14:19+00:00","article_modified_time":"2024-09-10T15:46:02+00:00","author":"Middleware Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Middleware Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/"},"author":{"name":"Middleware Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"headline":"Configure Zabbix with Ansible","datePublished":"2024-02-13T13:14:19+00:00","dateModified":"2024-09-10T15:46:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/"},"wordCount":399,"commentCount":2,"keywords":["Ansible","Monitoring","Zabbix"],"articleSection":["Ansible","DevOps"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/","url":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/","name":"Configure Zabbix with Ansible - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2024-02-13T13:14:19+00:00","dateModified":"2024-09-10T15:46:02+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/configure-zabbix-with-ansible\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Configure Zabbix with Ansible"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1","name":"Middleware Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","caption":"Middleware Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/30881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=30881"}],"version-history":[{"count":13,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/30881\/revisions"}],"predecessor-version":[{"id":30908,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/30881\/revisions\/30908"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=30881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=30881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=30881"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=30881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}