Ansible 企业zabbix实战
# Ansible 企业zabbix实战
# 掌握要点
(1)熟悉ansible的playbook的用法以及role的用法。
(2)熟悉如何使用ansible部署zabbix监控系统。
# 1.节点规划
IP地址 | 主机名称 | 节点名称 |
---|---|---|
192.168.1.4 | ansible | Ansible |
192.168.1.11 | zabbix | Zabbix |
# 2.查看当前的目录
[root@ansible gpmall_ansible]# ls
group_vars install_gpmall_cluster.yaml roles
[root@ansible gpmall_ansible]#
[root@ansible gpmall_ansible]#
[root@ansible gpmall_ansible]# tree roles/zabbix/
roles/zabbix/
├── files
│ ├── yum.repo
│ └── zabbix.tar.gz
├── handlers
├── meta
├── tasks
│ └── main.yaml
├── templates
│ └── zabbix_server.conf.j2
└── vars
6 directories, 4 files
# 3.编写zabbix角色
⚫该角色的作用是在 Zabbix 节点上安装 Zabbix 监控服务。首先要配置基础环 境,安装 LAMP 环境,最后部署 Zabbix 监控系统。
⚫在 roles/zabbix/tasks 目录下,创建 main.yaml 文件。
[root@ansible gpmall_ansible]# cat roles/zabbix/tasks/main.yaml
- name: selinux config
shell: "{{item}}"
with_items:
- sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
- systemctl stop firewalld
- name: config yum
shell: mv /etc/yum.repos.d/* /media
- name: copy repo
copy:
src: yum.repo
dest: /etc/yum.repos.d/yum.repo
- name: tar gz
unarchive:
src: zabbix.tar.gz
dest: /opt
- name: install packages
yum:
name:
- httpd
- mariadb
- mariadb-server
state: present
- name: servce start
systemd:
name: "{{item}}"
state: restarted
enabled: yes
with_items:
- httpd
- mariadb
- name: config mariadb user
shell: mysqladmin -uroot password {{DB_PASS}}
ignore_errors: yes
- name: mariadb create zabbix
shell: "{{item}}"
ignore_errors: yes
with_items:
- mysql -uroot -p{{DB_PASS}} -e "create database zabbix character set utf8 collate utf8_bin;"
- mysql -uroot -p{{DB_PASS}} -e "grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix';"
- mysql -uroot -p{{DB_PASS}} -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';"
- name: install zabbix
yum:
name:
- zabbix-server-mysql
- zabbix-web-mysql
- zabbix-agent
- trousers
state: present
- name: sql config mariadb
shell: chdir=/usr/share/doc/zabbix-server-mysql-3.4.15/ zcat create.sql.gz | mysql -uroot -p{{DB_PASS}} zabbix
ignore_errors: yes
- name: php time
shell: "{{item}}"
with_items:
- sed -i "s/^;date.timezone.*/;date.timezone=RPC/g" /etc/php.ini
- sed -i "s/#\ php_value/php_value/g" /etc/httpd/conf.d/zabbix.conf
- sed -i "s/date.timezone.*/date.timezone Asia\/Shanghai/g" /etc/httpd/conf.d/zabbix.conf
- name: config zabbix
template:
src: zabbix_server.conf.j2
dest: /etc/zabbix/zabbix_server.conf
- name: restart httpd and zabbix-server
systemd:
name: "{{item}}"
state: restarted
enabled: yes
with_items:
- httpd
- zabbix-server
# 4.添加变量
⚫在roles的相同目录下面创建一个目录叫做**group_vars,**创建文件all,将所有变量写入。
⚫添加变量在all中。
[root@ansible gpmall_ansible]# cat group_vars/all
···
DB_PASS: 123456
DB_HOST: 127.0.0.1
# 5.编写role角色
⚫通过执行zabbix角色来快速部署zabbix。
⚫编写zabbix的role角色作为整个控制playbook的主要领导。
[root@ansible gpmall_ansible]# cat install_gpmall_cluster.yaml
---
- hosts: zabbix
remote_user: root
roles:
- zabbix
# 6.执行palybook
[root@ansible gpmall_ansible]# ansible-playbook install_gpmall_cluster.yaml
PLAY [zabbix] ****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [192.168.1.11]
TASK [zabbix : config yum] ***************************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [zabbix : copy repo] ****************************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [zabbix : tar gz] *******************************************************************************************************************************************************************************************
anged: [192.168.1.11]
▽
TASK [zabbix : install packages] *********************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [zabbix : servce start] *************************************************************************************************************************************************************************************
changed: [192.168.1.11] => (item=httpd)
changed: [192.168.1.11] => (item=mariadb)
TASK [zabbix : config mariadb user] ******************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [mariadb create zabbix] *************************************************************************************************************************************************************************************
changed: [192.168.1.11] => (item=mysql -uroot -p123456 -e "create database zabbix character set utf8 collate utf8_bin;")
changed: [192.168.1.11] => (item=mysql -uroot -p123456 -e "grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix';")
changed: [192.168.1.11] => (item=mysql -uroot -p123456 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';")
TASK [install zabbix] ********************************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [zabbix : sql config mariadb] *******************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [zabbix : php time] *****************************************************************************************************************************************************************************************
changed: [192.168.1.11] => (item=sed -i "s/^;date.timezone.*/;date.timezone=RPC/g" /etc/php.ini)
changed: [192.168.1.11] => (item=sed -i "s/#\php_value/php_value/g" /etc/httpd/conf.d/zabbix.conf)
changed: [192.168.1.11] => (item=sed -i "s/date.timezone.*/date.timezone Asia\/Shanghai/g" /etc/httpd/conf.d/zabbix.conf)
[WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'. If you need to use command because replace, lineinfile or template is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
TASK [config zabbix] *********************************************************************************************************************************************************************************************
changed: [192.168.1.11]
TASK [restart httpd and zabbix-server] ***************************************************************************************************************************************************************************
changed: [192.168.1.11] => (item=httpd)
changed: [192.168.1.11] => (item=zabbix-server)
PLAY RECAP *******************************************************************************************************************************************************************************************************
192.168.1.11 : ok=13 changed=12 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
最后访问http://IP/zabbix
上次更新: 2023/11/28, 22:03:59