咱們這裏使用的是ansible來對zabbix-agent進行批量部署,固然在Linux上也可使用腳原本完成部署html
ansible:10.127.0.133 agent1:172.168.0.4 agent2:172.168.0.5
爲方便ansible對agent主機的管理,須要將ansible與agent進行公鑰認證來實現免密登錄 python
ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.4 ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.5
[Linux-agent] 172.168.0.4 172.168.0.5
建立一個模板文件,裏面包含agent中可變的變量,如:主機名和server地址linux
[root@zabbix-server ~]# vim /etc/ansible/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server={{server}} ServerActive={{server}} Hostname={{hostname}} Include=/etc/zabbix/zabbix_agentd.d/*.conf UnsafeUserParameters=1
vim /etc/ansible/linux-agent.yml - hosts: zabbix-agent remote_user: root vars: server: 10.127.0.133 hostname: "{{ ansible_hostname }}" tasks: - name: install rpm command: rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.1-1.el7.x86_64.rpm - name: install agent command: yum install zabbix-agent -y - name: cp templates zabbix_agentd.conf to zabbix agentd template: src=/etc/ansible/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf - name: restart zabbix-agent command: systemctl restart zabbix-agent
ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml git
能夠看到playbook已經執行成功了,接下來能夠看一下agent的配置文件 github
能夠看到,agent配置文件中的變量也修改完成shell
能夠看到自動發現規則生效了,並連接了Linux-OS模板 vim
Windows下的批量部署能夠經過配置管理工具或者域控制器進行,這裏我使用的ansible來對Windows主機進行批量部署windows
ansible:10.127.0.133 Windows server2012:172.168.0.6
pywinrm>=0.3.0
pywinrm可使用pip來進行安裝,執行如下命令ssh
pip install pywinrm>=0.3.0
PowerShell 3.0 NET Framework 4.0+
我這裏使用的是2012,上面的環境是不須要作配置的,若是是使用的server2008或更低版本須要進行升級以後才能使用,獲取升級的詳細信息能夠訪問ansible官方文檔查看
https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html#host-requirementside
因爲ansible控制Windows不是使用的ssh協議,而是用的Windows的winrm服務,而winrm有一個限制可用內存量的錯誤,須要安裝腳本進行修復
在powershell上執行下面的命令
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1" $file = "$env:temp\Install-WMF3Hotfix.ps1" (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file) powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
能夠在powershell上執行下面的命令查看winrm當前的監聽端口
winrm enumerate winrm/config/Listener
winrm服務默認是5985端口,zabbix-agent使用的是10050端口,所以須要在防火牆上開啓5985和10050端口或直接關閉防火牆
首先須要下載Windows-agent的壓縮包並解壓到ansible主機下
下載地址:https://www.zabbix.com/download_agents
須要在hosts中指定與Windows鏈接的配置信息,默認狀況下使用ntlm認證,若是想要獲取關於winrm認證的詳細信息,能夠訪問https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html
[windows] 172.168.0.6 ansible_python_interpreter=/usr/bin/python ansible_user="administrator" ansible_password="asd.123" ansible_port=5985 ansible_connection="winrm" ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore
vim /etc/ansible/windows-agent.yml - hosts: windows remote_user: administrator vars: server: 10.127.0.133 hostname: "{{ ansible_host }}" tasks: - name: cp zabbix-agent win_copy: src: /etc/ansible/windows_agent/ dest: C:\windows_agent\ - name: cp templates zabbix_agentd.conf to zabbix agentd win_template: src: /etc/ansible/zabbix_agentd.conf dest: C:\windows_agent\conf\ - name: install zabbix-agent win_command: zabbix_agentd.exe -i -c C:\windows_agent\conf\zabbix_agentd.conf args: chdir: C:\windows_agent\bin\ - name: start zabbix-agent win_command: zabbix_agentd.exe -s -c C:\windows_agent\conf\zabbix_agentd.conf args: chdir: C:\windows_agent\bin\
ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml
能夠看到playbook執行成功了,查看Windows的服務,Zabbix-agent也已經啓動
能夠看到自動發現規則生效了,並連接了Windows-OS模板
歡迎各×××陳師傅」