1.ansible目录结构
ansible_workspace
├── environments
│ ├── colony
│ │ ├── inventory
│ │ └── vars.yml
├── roles
│ └── zabbix_agent_windows_install
│ ├── files
│ │ └── zabbix_agent.zip
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ └── zabbix_agentd.conf.j2
└── windows_install.yml
1.1 inventory
[windows]
1.1.1.1 ansible_ssh_user="域\账号" ansible_ssh_pass="密码" ansible_ssh_port=5985 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm
2.2.2.2 ansible_ssh_user="域\账号" ansible_ssh_pass="密码" ansible_ssh_port=5985 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=ntlm
1.2 vars.yml
暂无相关配置
1.3 zabbix_agent.zip
#目录结构如下,为 版本zabbix_agent-5.4.9-windows-amd64.zip稍微改造了下
├── bin
│ ├── dev
│ │ └── zabbix_sender.dll
│ ├── zabbix_get.exe
│ └── zabbix_sender.exe
├── conf
│ ├── zabbix_agentd.conf
│ └── zabbix_agentd.conf.d
├── logs
└── sbin
└── zabbix_agentd.exe
1.4 main.yml
---
- name: judge zabbix_agent exits
win_shell: ls c:\dev\zabbix_agent
ignore_errors: True
register: zabbix_agent
tags: judge
- name: kill zabbix_agent
win_shell: taskkill /f /t /im zabbix_agentd.exe
ignore_errors: True
tags: stop
when: zabbix_agent is success
- name: create workdir
win_file:
path: "{{ item }}"
state: directory
with_items:
- c:\dev\zabbix_agent
when: zabbix_agent is failed
- name: cp file zabbix_agent.zip
tags: cp_package
win_copy:
src: /home/jenkins/ansible_workspace/roles/zabbix_agent_windows_install/files/zabbix_agent.zip
dest: c:\dev\zabbix_agent\zabbix_agent.zip
when: zabbix_agent is failed
- name: unarchive package zabbix_agent
win_unzip:
src: c:\dev\zabbix_agent\zabbix_agent.zip
dest: c:\dev\zabbix_agent
copy: yes
when: zabbix_agent is failed
- name: cp file zabbix.conf
tags: cp_template
win_template:
src: zabbix_agentd.conf.j2
dest: c:\dev\zabbix_agent\conf\zabbix_agentd.conf
#- name: unarchive package script
# win_unzip:
# src: script.zip
# dest: c:\dev\zabbix_agent\bin\
# copy: yes
#- name: cp file discovery.conf
# tags: cp_template
# win_template:
# src: discovery.conf.j2
# dest: "c:\dev\zabbix_agent\conf\zabbix_agentd.conf.d"
# ignore_errors: True
- name: add service
tags: add_service
win_shell: c:\dev\zabbix_agent\sbin\zabbix_agentd.exe -i -c c:\dev\zabbix_agent\conf\zabbix_agentd.conf
when: zabbix_agent is failed
- name: start zabbix
tags: start
win_shell: c:\dev\zabbix_agent\sbin\zabbix_agentd.exe -c c:\dev\zabbix_agent\conf\zabbix_agentd.conf -s
- name: add firmwall
tags: add_firmwall
win_shell: netsh advfirewall firewall add rule name="zabbix_agent" dir=in action=allow protocol=TCP localport=10055 enable=yes remoteip="zabbixserver地址" profile=domain
when: zabbix_agent is failed
1.5 zabbix_agentd.conf.j2
LogFile=c:\dev\zabbix_agent\logs\zabbix_agentd.log
LogFileSize=0
ListenPort=10055
StartAgents=3
ListenIP=0.0.0.0
Server="zabbixserver地址"
ServerActive="zabbixserver地址"
Hostname={{ ansible_host }}
Include=c:\dev\zabbix_agent\conf\zabbix_agentd.conf.d\
2.jenkins job配置
此job可循环执行,第一次执行时部署,后面再执行时 只会推送配置,并重启服务
2.1 执行shell部分
ansibleHome='/home/jenkins/ansible_workspace'
cd ${ansibleHome}
ansible-playbook windows_install.yml -i environments/${environment}/inventory -e "hosts=${hosts} env=${environment} ansibleHome=${ansibleHome} util=zabbix_agent_windows_install"