Ansible批量在Windows server上部署zabbix agent(有代码脚本)

一、前提

1.ansible节点--centos7Python3环境(centos8,redhat8以上自带python3)

centos7需要自行下载python3,注意python3版本与ansible版本的兼容性

2.主机--windows server2016

关闭防火墙,但需要设置允许相应流量进入规则

可以远程连接--winrm

二、部署配置

1.windows server

1.1关闭防火墙

1.2远程管理winrm

服务器管理器-->添加角色和功能-->安装的角色服务-远程桌面主机-->默认下一步

确保 WinRM 已经在 Windows 主机上启用。

1.3配置winrm 

PS C:\Users Administrator> winrm set winrm/config/service '@{AllowUnencrypted="true"}'
PS C:\Users Administrator> winrm set winrm/config/service/auth '@{Basic="true"}'

2.ansible节点

2.1配置python3环境

[root@localhost ~]# yum install -y python3 python3-pip 
[root@localhost ~]# mkdir myproject

pip3安装virtualenv虚拟环境管理工具、pywinrm模块

创建虚拟环境

 激活虚拟环境(退出执行deactivate)

[root@localhost ~]# pip install virtualenv pywinrm
[root@localhost myproject]# python3 -m venv myvenv
[root@localhost myproject]# source myvenv/bin/activate

2.2安装ansible并与Windows连接成功

注意:不要安装太新的版本,可以使用pip search查看可用版本

pip3安装ansible

 配置ansible(暂时需要inventory、 ansible.cfg)

创建主机清单文件inventory

我这块使用的是最简易的配置,不安全验证,http连接

[windows]
win_host ansible_host=winodws ip地址 ansible_user=windows的用户名 ansible_password=密码 ansible_connection=winrm ansible_port=5985 ansible_winrm_scheme=http ansible_winrm_server_ cert_validation=ignore

创建配置文件ansible.cfg(可以之间从/etc/ansible/ansible.cfg复制到当前目录)

暂时只修改主机清单文件的路径

测试

Ansible批量在Windows server上部署zabbix agent(有代码脚本)_第1张图片

 三、编写playbook(仅供参考)

注:保证节点上有zaabix-agent的安装包

1. install_playbook.yml

在Windows上检查是否有zabbix agent服务,如果有,重启服务并退出playbook,如果没有,上传,解压,安装部署zabbix agent服务。

---
- hosts: windows
  gather_facts: false
  vars:
    zabbix_server_ip: "192.168.140.128"
    zabbix_server_active_ip: "192.168.140.128"
    zabbix_hostname: "Win-Server"
  tasks:
    - name: Check if Zabbix Agent service exists
      win_shell: Get-Service -Name "Zabbix Agent"
      ignore_errors: yes
      register: zabbix_agent_service_check

    - name: Restart Zabbix Agent service if it exists
      win_shell: Restart-Service -Name "Zabbix Agent"
      when: zabbix_agent_service_check.rc == 0
      ignore_errors: yes

    - name: Exit playbook if Zabbix Agent service exists
      meta: end_play
      when: zabbix_agent_service_check.rc == 0

    - name: Check if C:\Temp exists
      win_stat:
        path: C:\Temp
      register: temp_directory

    - name: Create C:\Temp if it does not exist
      win_file:
        path: C:\Temp
        state: directory
      when: not temp_directory.stat.exists

    - name: Upload Zabbix Agent package
      win_copy:
        src: zabbix_agent-5.2.7-windows-amd64-openssl.zip
        dest: "C:\\Temp\\zabbix_agent-5.2.7-windows-amd64-openssl.zip"
      when: zabbix_agent_service_check.rc != 0  

    - name: Unzip Zabbix Agent package
      win_unzip:
        src: C:\Temp\zabbix_agent-5.2.7-windows-amd64-openssl.zip
        dest: "C:\\Temp\\"
      notify: Restart Zabbix Agent
      when: zabbix_agent_service_check.rc != 0  

    - name: Configure Zabbix Agent
      win_template:
        src: zabbix_agentd.conf.j2
        dest: "C:\\Temp\\conf\\zabbix_agentd.conf"
      when: zabbix_agent_service_check.rc != 0  

    - name: Install Zabbix Agent
      win_shell: |
        Start-Process -FilePath "C:\\Temp\\bin\\zabbix_agentd.exe" -ArgumentList "-c C:\\Temp\\conf\\zabbix_agentd.conf -i" -Wait
      notify: Restart Zabbix Agent
      when: zabbix_agent_service_check.rc != 0  

  handlers:
    - name: Restart Zabbix Agent
      win_shell: Start-Service -Name "Zabbix Agent"

2. zabbix_agentd.conf.j2


Server={{ zabbix_server_ip }}
ServerActive={{ zabbix_server_active_ip }}
Hostname={{ zabbix_hostname }}
LogFile=C:\Temp\zabbix_agentd.log
LogFileSize=1
DebugLevel=3

3.执行playbook文件

(myvenv) [root@localhost ansible]# ansible-playbook windows install_playbook.yml

你可能感兴趣的:(运维,ansible,zabbix,windows)