Ansible playbook for managing Windows / Linux Zabbix agents

This article is about writing simple ansible playbooks to automatically install agents on Linux / Windows hosts and register hosts through the Zabbix API, including SNMP hosts. Ansible Galaxy Zabbix ready-made roles and modules will be used.





Zabbix has prepared its own roles and modules for configuring many Zabbix components via Ansible - a complete list can be found here .





In this article, we will only talk about zabbix_agent and zabbix_host .





* This article does not cover or show the installation and configuration of Ansible, there is a lot of this on the Internet.





zabbix_agent





This is a role that supports the installation of the agent on the following operating systems:





  • Red Hat





  • Fedora





  • Debian





  • Ubuntu





  • opensuse





  • Windows (Best effort)





  • macOS





An up-to-date list on the official github of the project .





zabbix_host





This is a module for adding / removing / changing hosts on zabbix server. The full description of the module is here .





So let's move on to setting up

The first step is to install the ansible collection (only works in ansible version 2.9+):





ansible-galaxy collection install community.zabbix
      
      



After installing the collection, create a file with a playbook. This playbook does two things - it sets the community.zabbix.zabbix_agent role to the specified hosts and in the second step registers the host in zabbix via the community.zabbix.zabbix_host module.





zabbix-agent-all.yaml :





- hosts: all 
  tasks:
    - name: Install agent
      include_role:
        name:  community.zabbix.zabbix_agent #     
      tags:
        - install #     
    - name: Create a new host or update an existing host's info #      zabbix 
      local_action:
        module: community.zabbix.zabbix_host #    zabbix_host   
        server_url: "{{ zabbix_url }}" #    - url    API
        login_user: "{{ zabbix_api_user }}" #    -    Admin
        login_password: "{{ zabbix_api_pass }}" #    -  
        host_name: "{{ item }}" - #    
        visible_name: "{{ hostvars[item].zabbix_visible_name | default(item) }}" #        
        description: "{{   hostvars[item].zabbix_host_description | default('') }} OS: {{  hostvars[item].ansible_distribution | default('') }} {{  hostvars[item].ansible_distribution_version | default('') }}" #               ansible
        host_groups: "{{ hostvars[item].zabbix_host_groups }}" #      
        link_templates: "{{ hostvars[item].zabbix_link_templates }}" #  template   
        status: "{{  hostvars[item].zabbix_host_status }}" #  - Enabled  Disabled
        state: present #  ansible   ,   absent     
        inventory_mode: disabled #   inventory mode
        interfaces: #    
          - type: "{{ hostvars[item].zabbix_interface_type }}" #    -   SNMP, Agent, JMX, IPMI
            main: 1 
            useip: "{{ hostvars[item].zabbix_interface_use_ip }}" #     ip   dns
            ip: "{{ hostvars[item].zabbix_interface_ip }}" #    ip -    ip 
            dns: "{{ item }}" #   useip: 0    FQDN     
            port: "{{ hostvars[item].zabbix_interface_port }}" #  
      loop: "{{ groups['all'] }}" #        
      run_once: true #      ,                 
      tags:
        - add-host #     
      
      



, zabbix http, .., .





linux-inventory, windows-inventory snmp-inventory, , .





Linux





linux-inventory:





[all]
host1.local #      FQDN  IP-
host2.local  zabbix_host_description=”The host2 description” 
host3.local zabbix_host_status=disabled #       
host4.local zabbix_host_groups=’["Custom group"]’ #      
[all:vars]
zabbix_agent_server=my.zabbix.server #  
zabbix_url=https://my.zabbix.server # URL 
zabbix_api_use=true #      API
zabbix_api_user=Admin
zabbix_api_pass=StrongPa$$w0rd
zabbix_interface_port="10050" #     
zabbix_host_groups=["Linux servers"] #       
zabbix_link_templates=["Linux by Zabbix agent"] #  template    
zabbix_interface_type=agent #   -   
zabbix_interface_use_ip="0" #      dns fqdn
zabbix_interface_ip=""
zabbix_host_description="My linux server" #   
zabbix_host_status=enabled #    

      
      



Windows





Windows ansible - , ansible.





windows-inventory:





[all]
winhost01.local
winhost02.local  zabbix_host_groups=’["Custom group"]’ #      
winhost03.local zabbix_host_status=disabled #       
[all:vars]
ansible_user=user@DOMAIN.LOCAL #        ,    kerberos  linux
ansible_password=StrongPa$$w0rd
ansible_connection=winrm #  windows    WinRM
ansible_winrm_server_cert_validation=ignore #      ,   ignore
zabbix_agent_server=my.zabbix.server #  
zabbix_url=https://my.zabbix.server # URL 
zabbix_api_use=true #      API
zabbix_api_user=Admin
zabbix_api_pass=StrongPa$$w0rd
zabbix_interface_port="10050" #     
zabbix_host_groups=["Windows servers"]  #       
zabbix_link_templates=["Windows by Zabbix agent"] #  template    
zabbix_interface_type=agent #   -   
zabbix_interface_use_ip="0" #      dns fqdn
zabbix_interface_ip=""
zabbix_host_description="My windows server" #   
zabbix_host_status=enabled #    

      
      



SNMP 





SNMP snmp-inventory:





[all]
snmphost1.local zabbix_link_templates='["Cisco IOS SNMP","Network Generic Device SNMP"]' zabbix_host_groups='["Network devices"]'
snmp2.local zabbix_link_templates='["Template SNMP OS ESXi"]' zabbix_host_groups='["Hypervisors"]' zabbix_snmp_community=”Community”
[all:vars]
zabbix_url=https://my.zabbix.server # URL 
zabbix_api_use=true #      API
zabbix_api_user=Admin
zabbix_api_pass=StrongPa$$w0rd
zabbix_interface_port="161" #   ,   161
zabbix_interface_type=snmp #  snmp
zabbix_interface_use_ip="0" #     dns
zabbix_interface_ip="" #  ip
zabbix_host_description="My SNMP host" #   
zabbix_host_status=enabled #    
zabbix_snmp_community="MyCommunity" #  SNMP community

      
      



As a result, the files should have the following file structure:





.
β”œβ”€β”€ linux-inventory
β”œβ”€β”€ snmp-inventory
β”œβ”€β”€ windows-inventory
└── zabbix-agent-all.yaml

      
      



Launching a playbook

To run playbooks, you need to run the following commands:





To install on Linux hosts:





ansible-playbook -i linux-inventory zabbix-agent-all.yaml
      
      



Windows:





ansible-playbook -i windows-inventory zabbix-agent-all.yaml
      
      



SNMP:





ansible-playbook -i snmp-inventory zabbix-agent-all.yaml
      
      



If only installation is needed, then you can run the commands above with the install tag:





ansible-playbook -i linux-inventory zabbix-agent-all.yaml -t install
      
      



If you have special zabbix settings or want to customize, a complete list of role and module variables can be found on the project page: zabbix_agent and zabbix_host





All sources for this article are on github .








All Articles