简介
zabbix属于比较常用的运维监控系统。
服务器一多,我们一台台去部署配置就比较浪费时间了,可以使用ansiable批量进行部署。
ansiable playbook
playbook 是 ansible 用于配置,部署,和管理被控节点的剧本。
我们可以创建一个playbook来安装zabbix。
vim install_zabbix.yml
- name: 安装zabbix客户端 hosts: "{{ hosts }}" remote_user: root vars: - host: "{{ansible_host}}" - public_ip: "{{public_ip}}" - zabbix_server: "{{zabbix_server}}" - os_version: "{{ansible_distribution_major_version}}" tasks: - name: 备份 /etc/yum.repos.d/zabbix.repo command: mv -f /etc/yum.repos.d/zabbix.repo /tmp/zabbix.repo args: removes: /etc/yum.repos.d/zabbix.repo - name: 添加阿里云zabbix的镜像源 blockinfile: path: /etc/yum.repos.d/zabbix.repo create: yes mode: '644' block: | [zabbix] name=Zabbix Aliyun Repository - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/{{ os_version }}/$basearch/ enabled=1 gpgcheck=0 [zabbix-non-supported] name=Zabbix Aliyun Repository non-supported - $basearch baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/{{ os_version }}/$basearch/ enabled=1 gpgcheck=0 - name: 安装 zabbix 客户端和依赖工具 yum: name: - zabbix-agent state: installed - name: 修改zabbix配置文件 blockinfile: path: /etc/zabbix/zabbix.conf create: yes mode: '644' block: | Server={{ zabbix_server }} ServerActive={{ zabbix_server }} Hostname={{host}}_{{public_ip}} LogFile=/var/log/zabbix/zabbix-agent.log PidFile=/var/run/zabbix/zabbix_agentd.pid - name: 重启 zabbix-agnet service: name: zabbix-agent enabled: yes state: restarted
保存之后,就可以直接使用ansible-playbook这个命令直接去执行了。
使用
安装的时候需要传递几个变量,一个是公网IP,用于区分主机名的,zabbix服务端地址,用于自动配置客户端。
ansible-playbook -i /etc/ansible/hosts -e hosts=192.168.1.22 -e public_ip=1.1.1.1 -e zabbix_server=192.168.100.100 install_zabbix.yaml
总结
自动化运维,让工作更有效率。