{"id":345638,"date":"2023-02-18T21:01:47","date_gmt":"2023-02-18T21:01:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345638"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345638","title":{"rendered":"<span>\u0420\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043f\u0438\u0439 Zabbix \u043d\u0430 Ansible<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0432\u044b\u043f\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u043a\u0435 \u043a\u043e\u043f\u0438\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e Zabbix-server \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u0448\u0438\u043d, \u0434\u0430\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445, \u0434\u0430 \u0438 \u0435\u0449\u0435 \u0432\u0441\u0443\u043d\u0443\u0442\u044c \u044d\u0442\u043e \u0432 CI\/CD GitLab. \u0412\u0437\u044f\u043b Ansible \u0438 \u043d\u0430\u0447\u0430\u043b \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043b\u0435\u0439\u0431\u0443\u043a.<\/p>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u043c\u0438 \u043d\u0430 \u0445\u043e\u0441\u0442\u044b Zabbix&#8217;\u0430 \u0447\u0435\u0440\u0435\u0437 Ansible<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0444\u0430\u0439\u043b \u0445\u043e\u0441\u0442\u043e\u0432 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0449\u0438\u043a\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u0430\u0445 Linux(Debian, Ubuntu, RedHat):<\/p>\n<pre><code>[remote_zabbix:children] os_family_Debian os_family_RedHat os_family_Ubuntu py_script  ## current_host - \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 = \u0441\u0430\u043c\u043e\u043c\u0443 \u0445\u043e\u0441\u0442\u0443(\u0434\u0430, \u0445\u043e\u0441\u0442 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c)  [py_script] #\u0421\u043a\u0440\u0438\u043f\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430\/\u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0433\u0438\u0442\u0430# current_host=  [os_family_Debian] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 Debian# current_host=  [os_family_RedHat] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 ReHat# current_host=  [os_family_Ubuntu] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 Ubuntu# current_host=  [os_family_Debian:vars] os_family_type=debian  [os_family_RedHat:vars] os_family_type=redhat  [os_family_Ubuntu:vars] os_family_type=ubuntu <\/code><\/pre>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043b \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043f\u0438\u043d\u0433 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432:<\/p>\n<pre><code class=\"yaml\">- name: Test Connection   hosts: remote_zabbix   become: yes    tasks:     - name: Ping server       ping:   tags:     - test_connection<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u043b\u0441\u044f, \u0447\u0442\u043e \u0445\u043e\u0441\u0442\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b, \u0441\u043e\u0437\u0434\u0430\u043b \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u044f Zabbix (web, server \u0438 \u0411\u0414 timescaledb \u043d\u0430 PostgreSQL) \u0438 \u0432\u044b\u0437\u0432\u0430\u043b \u0435\u0433\u043e \u0441 \u0440\u043e\u043b\u044c\u044e(<em>run_docker-compose<\/em>), \u0433\u0434\u0435 \u0441\u0442\u043e\u043f\u044f\u0442\u0441\u044f \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0441\u043e \u0441\u0442\u0430\u0440\u044b\u043c Zabbix&#8217;\u043e\u043c \u0438 \u0411\u0414, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043f\u043e\u0434\u043d\u044f\u0442\u044b, \u0438 \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u043d\u043e\u0432\u043e \u0432 docker-compose:<\/p>\n<pre><code class=\"yaml\">- name: rm zabbix-containers   ansible.builtin.shell: docker ps --filter name=zabbix* -aq | xargs docker stop | xargs docker rm   ignore_errors: True - name: rm timescale container   ansible.builtin.shell: docker ps --filter name=timescale* -aq | xargs docker stop | xargs docker rm   ignore_errors: True - name: run zabbix-server   ansible.builtin.shell: cd \/home &amp;&amp; docker-compose up -d <\/code><\/pre>\n<p>*\u0414\u0430\u0431\u044b \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0448\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u043d\u043e\u043f\u043e\u0441\u0442, \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043d\u0430\u043a\u0430\u0442\u044b\u0432\u0430\u044e Zabbix-agent2, \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u044f \u0435\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u044b \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432, \u0440\u043e\u043b\u044c &#8212; <em>install_agent2.service:<\/em><\/p>\n<pre><code class=\"yaml\">- name: Download agent2.service debian   get_url:     url: https:\/\/repo.zabbix.com\/zabbix\/6.2\/debian\/pool\/main\/z\/zabbix-release\/zabbix-release_6.2-4+debian10_all.deb     dest: .\/zabbix-release_6.2-4+debian10_all.deb   when: os_family_type == \"debian\"  - name: Download agent2.service debian   shell: |      dpkg -i .\/zabbix-release_6.2-4+debian10_all.deb     apt update   when: os_family_type == \"debian\"  - name: Install agent2.service debian   apt:     name: zabbix-agent2     update_cache: yes   when: os_family_type == \"debian\"  - name: Download agent2 redhat   yum:     name: https:\/\/repo.zabbix.com\/zabbix\/6.2\/rhel\/7\/x86_64\/zabbix-release-6.2-3.el7.noarch.rpm   when: os_family_type == \"redhat\"  - name: Yum clean all   shell: yum clean all   when: os_family_type == \"redhat\"  - name: Install agent2.service redhat   yum:     name: zabbix-agent2   when: os_family_type == \"redhat\" <\/code><\/pre>\n<p>\u041d\u0430\u043a\u0438\u0434\u044b\u0432\u0430\u044e \u0443\u0436\u0435 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0430 \u0430\u0433\u0435\u043d\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 (\u0442\u0435\u0433 <em>configure_zabbix-agent2<\/em>) \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0441\u043b\u0443\u0436\u0431\u0443 \u044d\u0442\u043e\u0433\u043e \u0430\u0433\u0435\u043d\u0442\u0430 \u0440\u043e\u043b\u044c\u044e <em>restart_agent2.service<\/em>.<\/p>\n<h2>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0438\u043c\u043f\u043e\u0440\u0442\u0430\/\u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c, \u0433\u0434\u0435 \u044f \u0434\u0443\u043c\u0430\u043b, \u043a\u0430\u043a \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442\/\u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u0430\u043c\u043e\u0433\u043e Zabbix, \u043a\u043e\u0433\u0434\u0430 \u0443\u0436\u0435 \u0432\u0441\u0435 \u043f\u043e\u0434\u043d\u044f\u0442\u043e. \u041f\u043e\u0438\u0441\u043a\u0430\u0432 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435, \u044f \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u0440\u0430\u0441\u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438, \u043b\u0438\u0431\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0439 API. \u041d\u043e, \u043a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u044f \u043e\u0442\u044b\u0441\u043a\u0430\u043b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f Python pyzabbix, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0430 (\u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043c, \u0441\u043f\u0443\u0441\u0442\u044f \u0432\u0440\u0435\u043c\u044f, \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438 \u0438\u043c\u043f\u043e\u0440\u0442).<\/p>\n<p>\u041d\u0430 \u0432\u0445\u043e\u0434 \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430: host_from(\u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433) \u0438 host_to(\u043a\u0443\u0434\u0430 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u043e\u043b\u0438 Ansible).<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 (\u043b\u043e\u0433\u0438\u043d-\u043f\u0430\u0440\u043e\u043b\u044c \u0438\u043b\u0438 \u0442\u043e\u043a\u0435\u043d):<\/p>\n<pre><code class=\"python\"># Auth Zabbix API to export def zabbix_auth_from(host_from):     zapi = ZabbixAPI(\"http:\/\/\" + host_from)     zapi.login(\"Admin\", \"zabbix\")     print(\"Connected to Zabbix API Version %s\" % zapi.api_version())     return zapi   # Auth Zabbix API to import def zabbix_auth_to(url_link):     zapi = ZabbixAPI('http:\/\/' + url_link + ':8080')     zapi.login(\"Admin\", \"zabbix\")     print(\"Connected to Zabbix API Version %s\" % zapi.api_version())     return zapi<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430\u043c \u0432\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043e\u0442\u0447\u0435\u0442 \u043e\u0431 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0432\u0445\u043e\u0434\u0435 \u0438 \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u0432\u0435\u0440\u0441\u0438\u044e Zabbix:<\/p>\n<blockquote>\n<p>Connected to Zabbix API Version %version%<\/p>\n<\/blockquote>\n<p>\u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0434\u0430\u043b\u0435\u0435 \u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0445\u043e\u0441\u0442\u043e\u0432, \u0433\u0440\u0443\u043f\u043f \u0445\u043e\u0441\u0442\u043e\u0432 \u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432:<\/p>\n<pre><code class=\"python\"># Func for make export files def write_export(name, config, export_format):     if config is not None:         print(\"Writing %s.%s\" % (name, export_format))         with open(\"%s.%s\" % (name, export_format), \"w\") as f:             f.write(config)   # Func for export hosts from main zabbix def export_hosts(zapi, namefile = \"export_zabbix_hosts\"):     hosts_ids = []     for item in zapi.host.get(output=\"extend\"):         hosts_ids.append(int(item['hostid']))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"hosts\": hosts_ids}     )     write_export(namefile, config, \"yml\")   # Func for export templates from main zabbix def export_templates(zapi, namefile = \"export_zabbix_templates\"):     hosts_ids = []     for item in zapi.template.get(output=\"extend\"):         hosts_ids.append(int(item['templateid']))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"templates\": hosts_ids}     )     write_export(namefile, config, \"yml\")   # Func for export host groups from main zabbix def export_host_groups(zapi, namefile = \"export_zabbix_hostgroups\"):     hosts_groups_ids = []     for item in zapi.hostgroup.get(output=\"extend\"):         hosts_groups_ids.append(int(item[\"groupid\"]))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"groups\": hosts_groups_ids}     )     write_export(namefile, config, \"yml\") <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430, \u043a\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c Zabbix \u043f\u043e API:<\/p>\n<pre><code class=\"python\">def export_zabbix_conf(zapi):     export_templates(zapi)     export_hosts(zapi)     export_host_groups(zapi)<\/code><\/pre>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c &#8212; \u044d\u0442\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u0438\u0435 \u0445\u043e\u0441\u0442\u044b Zabbix(\u0437\u0434\u0435\u0441\u044c zapi &#8212; \u0443\u0436\u0435 \u0445\u043e\u0441\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c):<\/p>\n<pre><code class=\"python\"># Func for import config to copy of zabbix def import_config(zapi, filename, rules):     print(\"Import \" + filename.split('_')[-1].split('.')[0])     export_file = open(filename, \"r\").read()     zapi.configuration['import'](format=\"yaml\", source=export_file, rules=rules)   def import_zabbix_conf(zapi):     import_config(zapi, \"export_zabbix_templates.yml\", { \"templates\": { \"createMissing\": True, \"updateExisting\": True }})     import_config(zapi, \"export_zabbix_hostgroups.yml\", { \"host_groups\": {\"createMissing\": True }})     import_config(zapi, \"export_zabbix_hosts.yml\", {\"hosts\": { \"createMissing\": True, \"updateExisting\": True }})<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u043a\u043e\u043d\u0444\u0438\u0433, \u043d\u043e \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 &#8212; \u0430\u0433\u0435\u043d\u0442 \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u0441\u0432\u043e\u0439 \u0436\u0435 \u0445\u043e\u0441\u0442 \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0437\u0430\u0431\u0431\u0438\u043a\u0441\u0430 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u0445\u043e\u0441\u0442 \u0441 &#171;Zabbix Server&#187; \u043d\u0430 \u043d\u0430\u0448 DNS, IP (\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043b \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 DNS, \u043c\u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0431\u044b\u043b\u043e):<\/p>\n<pre><code class=\"python\"># Func for rename localhost on zabbix-server, for find it by zabbix-agent2 def rename_local_host(zapi, remote_host):     try:         hid=zapi.host.get(search={'host': remote_host})[0]['hostid']     except:         hid=zapi.host.get(search={'host': 'Zabbix server'})[0]['hostid']     print('Change name')     zapi.host.update(hostid=hid, host=remote_host, name=remote_host)     print('Change DNS name')     iid = zapi.hostinterface.get(hostids=hid)[0]['interfaceid']     zapi.hostinterface.update(interfaceid=iid, dns=remote_host, useip=0, ip='\u0441\u044e\u0434\u0430 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 IP')  <\/code><\/pre>\n<h2>\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u0432 CI\/CD GitLab<\/h2>\n<p>\u041d\u0443 \u0437\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u041c\u044b \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u043d\u0438\u0439 \u0432 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0442\u0435\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0432 .gitlab-ci.yml:<\/p>\n<pre><code class=\"yaml\">image: ### Ansible 6.5.1 image  stages: - install\/update_zabbix - configure_zabbix-agent2 - run_script  Install\/update_Zabbix:   stage: install\/update_zabbix   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t install\/update_zabbix   when: manual  Configure_Zabbix-Agent2:   stage: configure_zabbix-agent2   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t configure_zabbix-agent2   when: manual  Export\/import_zabbix_config:   stage: run_script   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t run_sript -t restart-agent2   when: manual <\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u041c\u044b \u0441\u043c\u043e\u0433\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 Ansible \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0439 Zabbix-server \u0438 Zabbix-agent2 \u0441\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433 \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e Zabbix \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0445\u043e\u0441\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0434\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 pyzabbix.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439: <a href=\"https:\/\/github.com\/mksmp\/export-config-zabbix-ansible\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/mksmp\/export-config-zabbix-ansible<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717814\/\"> https:\/\/habr.com\/ru\/post\/717814\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0432\u044b\u043f\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u043a\u0435 \u043a\u043e\u043f\u0438\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e Zabbix-server \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u0448\u0438\u043d, \u0434\u0430\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445, \u0434\u0430 \u0438 \u0435\u0449\u0435 \u0432\u0441\u0443\u043d\u0443\u0442\u044c \u044d\u0442\u043e \u0432 CI\/CD GitLab. \u0412\u0437\u044f\u043b Ansible \u0438 \u043d\u0430\u0447\u0430\u043b \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043b\u0435\u0439\u0431\u0443\u043a.<\/p>\n<h2>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u043c\u0438 \u043d\u0430 \u0445\u043e\u0441\u0442\u044b Zabbix&#8217;\u0430 \u0447\u0435\u0440\u0435\u0437 Ansible<\/h2>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0444\u0430\u0439\u043b \u0445\u043e\u0441\u0442\u043e\u0432 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0442\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0449\u0438\u043a\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u0430\u0445 Linux(Debian, Ubuntu, RedHat):<\/p>\n<pre><code>[remote_zabbix:children] os_family_Debian os_family_RedHat os_family_Ubuntu py_script  ## current_host - \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 = \u0441\u0430\u043c\u043e\u043c\u0443 \u0445\u043e\u0441\u0442\u0443(\u0434\u0430, \u0445\u043e\u0441\u0442 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c)  [py_script] #\u0421\u043a\u0440\u0438\u043f\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0441\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430\/\u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0433\u0438\u0442\u0430# current_host=  [os_family_Debian] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 Debian# current_host=  [os_family_RedHat] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 ReHat# current_host=  [os_family_Ubuntu] #\u0425\u043e\u0441\u0442 \u043d\u0430 \u041e\u0421 Ubuntu# current_host=  [os_family_Debian:vars] os_family_type=debian  [os_family_RedHat:vars] os_family_type=redhat  [os_family_Ubuntu:vars] os_family_type=ubuntu <\/code><\/pre>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043b \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043f\u0438\u043d\u0433 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432:<\/p>\n<pre><code class=\"yaml\">- name: Test Connection   hosts: remote_zabbix   become: yes    tasks:     - name: Ping server       ping:   tags:     - test_connection<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u044f \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u043b\u0441\u044f, \u0447\u0442\u043e \u0445\u043e\u0441\u0442\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b, \u0441\u043e\u0437\u0434\u0430\u043b \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u044f Zabbix (web, server \u0438 \u0411\u0414 timescaledb \u043d\u0430 PostgreSQL) \u0438 \u0432\u044b\u0437\u0432\u0430\u043b \u0435\u0433\u043e \u0441 \u0440\u043e\u043b\u044c\u044e(<em>run_docker-compose<\/em>), \u0433\u0434\u0435 \u0441\u0442\u043e\u043f\u044f\u0442\u0441\u044f \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u044b \u0441\u043e \u0441\u0442\u0430\u0440\u044b\u043c Zabbix&#8217;\u043e\u043c \u0438 \u0411\u0414, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u043f\u043e\u0434\u043d\u044f\u0442\u044b, \u0438 \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0437\u0430\u043d\u043e\u0432\u043e \u0432 docker-compose:<\/p>\n<pre><code class=\"yaml\">- name: rm zabbix-containers   ansible.builtin.shell: docker ps --filter name=zabbix* -aq | xargs docker stop | xargs docker rm   ignore_errors: True - name: rm timescale container   ansible.builtin.shell: docker ps --filter name=timescale* -aq | xargs docker stop | xargs docker rm   ignore_errors: True - name: run zabbix-server   ansible.builtin.shell: cd \/home &amp;&amp; docker-compose up -d <\/code><\/pre>\n<p>*\u0414\u0430\u0431\u044b \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0430\u0448\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u043d\u043e\u043f\u043e\u0441\u0442, \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u044f \u043d\u0430\u043a\u0430\u0442\u044b\u0432\u0430\u044e Zabbix-agent2, \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u044f \u0435\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u044b \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432, \u0440\u043e\u043b\u044c &#8212; <em>install_agent2.service:<\/em><\/p>\n<pre><code class=\"yaml\">- name: Download agent2.service debian   get_url:     url: https:\/\/repo.zabbix.com\/zabbix\/6.2\/debian\/pool\/main\/z\/zabbix-release\/zabbix-release_6.2-4+debian10_all.deb     dest: .\/zabbix-release_6.2-4+debian10_all.deb   when: os_family_type == \"debian\"  - name: Download agent2.service debian   shell: |      dpkg -i .\/zabbix-release_6.2-4+debian10_all.deb     apt update   when: os_family_type == \"debian\"  - name: Install agent2.service debian   apt:     name: zabbix-agent2     update_cache: yes   when: os_family_type == \"debian\"  - name: Download agent2 redhat   yum:     name: https:\/\/repo.zabbix.com\/zabbix\/6.2\/rhel\/7\/x86_64\/zabbix-release-6.2-3.el7.noarch.rpm   when: os_family_type == \"redhat\"  - name: Yum clean all   shell: yum clean all   when: os_family_type == \"redhat\"  - name: Install agent2.service redhat   yum:     name: zabbix-agent2   when: os_family_type == \"redhat\" <\/code><\/pre>\n<p>\u041d\u0430\u043a\u0438\u0434\u044b\u0432\u0430\u044e \u0443\u0436\u0435 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0430 \u0430\u0433\u0435\u043d\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 (\u0442\u0435\u0433 <em>configure_zabbix-agent2<\/em>) \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0441\u043b\u0443\u0436\u0431\u0443 \u044d\u0442\u043e\u0433\u043e \u0430\u0433\u0435\u043d\u0442\u0430 \u0440\u043e\u043b\u044c\u044e <em>restart_agent2.service<\/em>.<\/p>\n<h2>\u0421\u043a\u0440\u0438\u043f\u0442 \u0434\u043b\u044f \u0438\u043c\u043f\u043e\u0440\u0442\u0430\/\u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c, \u0433\u0434\u0435 \u044f \u0434\u0443\u043c\u0430\u043b, \u043a\u0430\u043a \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u043c\u043f\u043e\u0440\u0442\/\u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0441\u0430\u043c\u043e\u0433\u043e Zabbix, \u043a\u043e\u0433\u0434\u0430 \u0443\u0436\u0435 \u0432\u0441\u0435 \u043f\u043e\u0434\u043d\u044f\u0442\u043e. \u041f\u043e\u0438\u0441\u043a\u0430\u0432 \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435, \u044f \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u0440\u0430\u0441\u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438, \u043b\u0438\u0431\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0439 API. \u041d\u043e, \u043a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u044f \u043e\u0442\u044b\u0441\u043a\u0430\u043b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f Python pyzabbix, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u043a\u043e\u043d\u0444\u0438\u0433\u0430 (\u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043c, \u0441\u043f\u0443\u0441\u0442\u044f \u0432\u0440\u0435\u043c\u044f, \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438 \u0438\u043c\u043f\u043e\u0440\u0442).<\/p>\n<p>\u041d\u0430 \u0432\u0445\u043e\u0434 \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430: host_from(\u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433) \u0438 host_to(\u043a\u0443\u0434\u0430 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c, \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u043e\u043b\u0438 Ansible).<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 (\u043b\u043e\u0433\u0438\u043d-\u043f\u0430\u0440\u043e\u043b\u044c \u0438\u043b\u0438 \u0442\u043e\u043a\u0435\u043d):<\/p>\n<pre><code class=\"python\"># Auth Zabbix API to export def zabbix_auth_from(host_from):     zapi = ZabbixAPI(\"http:\/\/\" + host_from)     zapi.login(\"Admin\", \"zabbix\")     print(\"Connected to Zabbix API Version %s\" % zapi.api_version())     return zapi   # Auth Zabbix API to import def zabbix_auth_to(url_link):     zapi = ZabbixAPI('http:\/\/' + url_link + ':8080')     zapi.login(\"Admin\", \"zabbix\")     print(\"Connected to Zabbix API Version %s\" % zapi.api_version())     return zapi<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043d\u0430\u043c \u0432\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043e\u0442\u0447\u0435\u0442 \u043e\u0431 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u043c \u0432\u0445\u043e\u0434\u0435 \u0438 \u0432\u044b\u0432\u0435\u0434\u0435\u0442 \u0432\u0435\u0440\u0441\u0438\u044e Zabbix:<\/p>\n<blockquote>\n<p>Connected to Zabbix API Version %version%<\/p>\n<\/blockquote>\n<p>\u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0434\u0430\u043b\u0435\u0435 \u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0445\u043e\u0441\u0442\u043e\u0432, \u0433\u0440\u0443\u043f\u043f \u0445\u043e\u0441\u0442\u043e\u0432 \u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432:<\/p>\n<pre><code class=\"python\"># Func for make export files def write_export(name, config, export_format):     if config is not None:         print(\"Writing %s.%s\" % (name, export_format))         with open(\"%s.%s\" % (name, export_format), \"w\") as f:             f.write(config)   # Func for export hosts from main zabbix def export_hosts(zapi, namefile = \"export_zabbix_hosts\"):     hosts_ids = []     for item in zapi.host.get(output=\"extend\"):         hosts_ids.append(int(item['hostid']))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"hosts\": hosts_ids}     )     write_export(namefile, config, \"yml\")   # Func for export templates from main zabbix def export_templates(zapi, namefile = \"export_zabbix_templates\"):     hosts_ids = []     for item in zapi.template.get(output=\"extend\"):         hosts_ids.append(int(item['templateid']))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"templates\": hosts_ids}     )     write_export(namefile, config, \"yml\")   # Func for export host groups from main zabbix def export_host_groups(zapi, namefile = \"export_zabbix_hostgroups\"):     hosts_groups_ids = []     for item in zapi.hostgroup.get(output=\"extend\"):         hosts_groups_ids.append(int(item[\"groupid\"]))      config = zapi.configuration.export(         format=\"yaml\", prettyprint=True, options={\"groups\": hosts_groups_ids}     )     write_export(namefile, config, \"yml\") <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430, \u043a\u0443\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c Zabbix \u043f\u043e API:<\/p>\n<pre><code class=\"python\">def export_zabbix_conf(zapi):     export_templates(zapi)     export_hosts(zapi)     export_host_groups(zapi)<\/code><\/pre>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c &#8212; \u044d\u0442\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u0438\u0435 \u0445\u043e\u0441\u0442\u044b Zabbix(\u0437\u0434\u0435\u0441\u044c zapi &#8212; \u0443\u0436\u0435 \u0445\u043e\u0441\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c):<\/p>\n<pre><code class=\"python\"># Func for import config to copy of zabbix def import_config(zapi, filename, rules):     print(\"Import \" + filename.split('_')[-1].split('.')[0])     export_file = open(filename, \"r\").read()     zapi.configuration['import'](format=\"yaml\", source=export_file, rules=rules)   def import_zabbix_conf(zapi):     import_config(zapi, \"export_zabbix_templates.yml\", { \"templates\": { \"createMissing\": True, \"updateExisting\": True }})     import_config(zapi, \"export_zabbix_hostgroups.yml\", { \"host_groups\": {\"createMissing\": True }})     import_config(zapi, \"export_zabbix_hosts.yml\", {\"hosts\": { \"createMissing\": True, \"updateExisting\": True }})<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u043a\u043e\u043d\u0444\u0438\u0433, \u043d\u043e \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 &#8212; \u0430\u0433\u0435\u043d\u0442 \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u0441\u0432\u043e\u0439 \u0436\u0435 \u0445\u043e\u0441\u0442 \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0437\u0430\u0431\u0431\u0438\u043a\u0441\u0430 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u0445\u043e\u0441\u0442 \u0441 &#171;Zabbix Server&#187; \u043d\u0430 \u043d\u0430\u0448 DNS, IP (\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043b \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 DNS, \u043c\u043d\u0435 \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0431\u044b\u043b\u043e):<\/p>\n<pre><code class=\"python\"># Func for rename localhost on zabbix-server, for find it by zabbix-agent2 def rename_local_host(zapi, remote_host):     try:         hid=zapi.host.get(search={'host': remote_host})[0]['hostid']     except:         hid=zapi.host.get(search={'host': 'Zabbix server'})[0]['hostid']     print('Change name')     zapi.host.update(hostid=hid, host=remote_host, name=remote_host)     print('Change DNS name')     iid = zapi.hostinterface.get(hostids=hid)[0]['interfaceid']     zapi.hostinterface.update(interfaceid=iid, dns=remote_host, useip=0, ip='\u0441\u044e\u0434\u0430 \u0432\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 IP')  <\/code><\/pre>\n<h2>\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u0432 CI\/CD GitLab<\/h2>\n<p>\u041d\u0443 \u0437\u0434\u0435\u0441\u044c \u0443\u0436\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u041c\u044b \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u043d\u0438\u0439 \u0432 \u043f\u043b\u0435\u0439\u0431\u0443\u043a\u0435 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0442\u0435\u0433\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0432 .gitlab-ci.yml:<\/p>\n<pre><code class=\"yaml\">image: ### Ansible 6.5.1 image  stages: - install\/update_zabbix - configure_zabbix-agent2 - run_script  Install\/update_Zabbix:   stage: install\/update_zabbix   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t install\/update_zabbix   when: manual  Configure_Zabbix-Agent2:   stage: configure_zabbix-agent2   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t configure_zabbix-agent2   when: manual  Export\/import_zabbix_config:   stage: run_script   script:     - ansible-playbook ansible\/zabbix_playbook.yml -i ansible\/hosts\/hosts.txt -vvvv -t run_sript -t restart-agent2   when: manual <\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u041c\u044b \u0441\u043c\u043e\u0433\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 Ansible \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0439 Zabbix-server \u0438 Zabbix-agent2 \u0441\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433 \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e Zabbix \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0445\u043e\u0441\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0434\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 pyzabbix.<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439: <a href=\"https:\/\/github.com\/mksmp\/export-config-zabbix-ansible\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/mksmp\/export-config-zabbix-ansible<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717814\/\"> https:\/\/habr.com\/ru\/post\/717814\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345638","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345638"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345638\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}