最近公司新上架了50台服务器,系统包括centos7.3和centos6.8,现在需要在这50台服务器中安装zabbix客户端,由于本人比较懒,所以就想到了自动化工具ansible。
具体是实现方法如下:
1、playbook代码如下:
[root@deploy playbook-test]# cat zabbix-agent.yml
- hosts: web remote_user: root gather_facts: yes vars: IPDD: "{{ ansible_default_ipv4['address'] }}" tasks: - name: 开始拷贝源码包 copy: #src: "/tmp/zabbix-2.2.23.tar.gz" #dest: "/tmp/zabbix-2.2.23.tar.gz" src: "{{ item.src }}" dest: "{{ item.dest }}" with_items: - { src: "/tmp/zabbix-2.2.23.tar.gz", dest: "/tmp/zabbix-2.2.23.tar.gz" } #- { src: "/tmp/zabbix-2.2.23.tar.gz", dest: "/tmp/zabbix-2.2.23.tar.gz" } #- { src: "/PATH/to/FILE", dest: "/tmp/zabbix-2.2.23.tar.gz" } - name: 开始解压源码包 unarchive: src: "/tmp/zabbix-2.2.23.tar.gz" dest: "/tmp" copy: no - name: 创建zabbix组 group: name: zabbix gid: 201 system: yes state: present - name: 创建zabbix用户 user: name: zabbix comment: Monitor User uid: 201 group: zabbix shell: /sbin/nologin - name: 安装GCC-C++ yum: name: gcc-c++ state: latest - name: 开始安装zabbix客户端 shell: cd /tmp/zabbix-2.2.23 && ./configure --sysconfdir=/etc/zabbix/ --sbindir=/usr/local/sbin/ --enable-agent && make && make install - name: 拷贝centos6启动脚本 copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: 0755 with_items: - { src: "/tmp/zabbix-2.2.23/misc/init.d/fedora/core5/zabbix_agentd", dest: "/etc/init.d/zabbix_agentd" } when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "6" - name: 拷贝centos7启动脚本 copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: 0644 with_items: - { src: "/tmp/zabbix_agentd.service", dest: "/lib/systemd/system/zabbix_agentd.service" } when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "7" - name: systemctl daemon-reload command: systemctl daemon-reload when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "7" - name: 拷贝配置文件 template: src: /tmp/zabbix_agentd.conf.j2 dest: /etc/zabbix/zabbix_agentd.conf tags: - change-conf notify: - restart zabbix-agent handlers: - name: restart zabbix-agent service: name=zabbix_agentd state=restarted enabled=yes
2、准备源码包及相关配置文件
root@deploy tmp]# ll
total 15160 drwxr-xr-x 13 1039 1039 4096 Jul 30 2018 zabbix-2.2.23 -rw-r--r-- 1 root root 15504361 Jul 4 01:28 zabbix-2.2.23.tar.gz -rw-r--r-- 1 root root 7465 Jul 6 01:48 zabbix_agentd.conf.j2 -rw-r--r-- 1 root root 412 Jul 6 01:13 zabbix_agentd.service
[root@deploy tmp]# grep -Ev "^#|^$" /etc/ansible/hosts
[web] 192.168.131.10 192.168.131.15 192.168.131.129
[root@deploy tmp]# grep -Ev "^#|^$" zabbix_agentd.conf.j2
PidFile=/tmp/zabbix_agentd.pid LogFile=/tmp/zabbix_agentd.log Server=192.168.131.100 ListenPort=10023 ServerActive=192.168.131.100 Hostname={{ IPDD }} Timeout=30
[root@deploy tmp]# cat zabbix_agentd.service
[Unit] Description=Zabbix Agent After=syslog.target After=network.target [Service] Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf" EnvironmentFile=-/etc/sysconfig/zabbix-agent Type=forking Restart=on-failure PIDFile=/tmp/zabbix_agentd.pid KillMode=control-group ExecStart=/usr/local/sbin/zabbix_agentd -c $CONFFILE ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s [Install] WantedBy=multi-user.target
3、测试ansible主机与客户端通信是否正常(ansible主机需要和客户端主机做ssh互信)
[root@deploy playbook-test]# ansible web -m ping
192.168.131.15 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.131.10 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.131.129 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
4、执行剧本(批量部署)
[root@deploy playbook-test]# ansible-playbook zabbix-agent.yml
5、测试客户端机器zabbix-agent是否成功安装并启动
[root@deploy playbook-test]# ansible web -m shell -a "ps -ef |grep zabbix_agentd|grep -v grep"
192.168.131.10 | CHANGED | rc=0 >> zabbix 26025 1 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf zabbix 26026 26025 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec] zabbix 26027 26025 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection] zabbix 26028 26025 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection] zabbix 26029 26025 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection] zabbix 26030 26025 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec] 192.168.131.15 | CHANGED | rc=0 >> zabbix 25956 1 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf zabbix 25957 25956 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec] zabbix 25958 25956 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection] zabbix 25959 25956 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection] zabbix 25960 25956 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection] zabbix 25961 25956 0 13:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: active checks #1 [idle 1 sec] 192.168.131.129 | CHANGED | rc=0 >> zabbix 11258 1 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd zabbix 11260 11258 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: collector [idle 1 sec] zabbix 11261 11258 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #1 [waiting for connection] zabbix 11262 11258 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #2 [waiting for connection] zabbix 11263 11258 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: listener #3 [waiting for connection] zabbix 11264 11258 0 06:49 ? 00:00:00 /usr/local/sbin/zabbix_agentd: active checks #1 [getting list of active checks][object Object]
结果显示在客户端机器上已经有zabbix_agentd的进程在运行。
6、如果需要变更配置内容,比如更改zabbix客户端的监听端口为10023,此时不需要将整个playbook都执行一遍可以在playbook相应的任务下加入tag,执行时使用“--tags”参数,指定要执行的playbook片段。
[root@deploy playbook-test]# grep -Ev "^#|^$" /tmp/zabbix_agentd.conf.j2
PidFile=/tmp/zabbix_agentd.pid LogFile=/tmp/zabbix_agentd.log Server=192.168.131.100 ListenPort=10023 ServerActive=192.168.131.100 Hostname={{ IPDD }} Timeout=30
[root@deploy playbook-test]# ansible-playbook zabbix-agent.yml --tags change-conf
PLAY [web] ************************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [192.168.131.15] ok: [192.168.131.10] ok: [192.168.131.129] TASK [拷贝配置文件] *********************************************************************************************************************************************** changed: [192.168.131.15] changed: [192.168.131.10] changed: [192.168.131.129] RUNNING HANDLER [restart zabbix-agent] ********************************************************************************************************************** changed: [192.168.131.15] changed: [192.168.131.10] changed: [192.168.131.129] PLAY RECAP ************************************************************************************************************************************************** 192.168.131.10 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.131.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.131.15 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0[object Object]
结果显示配置文件已替换成功,并且跳过了其他步骤,只执行了拷贝配置文件的任务,配置文件改变,触发了重启zabbix agent的handler。
7、查看客户端监听的端口是否发生了变化
[root@deploy playbook-test]# ansible web -m shell -a "netstat -antlp | grep zabbix "
192.168.131.15 | CHANGED | rc=0 >> tcp 0 0 0.0.0.0:10023 0.0.0.0:* LISTEN 26358/zabbix_agentd 192.168.131.10 | CHANGED | rc=0 >> tcp 0 0 0.0.0.0:10023 0.0.0.0:* LISTEN 26427/zabbix_agentd 192.168.131.129 | CHANGED | rc=0 >> tcp 0 0 0.0.0.0:10023 0.0.0.0:* LISTEN 11615/zabbix_agentd
ansible批量部署zabbix客户端到此结束,仅做记录,能力有限,存在很多不足,不喜勿喷。
后期更新通过ansible批量修改线上服务器用户密码。