ansible 学习笔记02-zabbix 安装
ansible 安装zabbix项目案例
1.基础环境准备:
[root@test ~]# ssh-keygen
[root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.7 #将公钥拷贝zabbix的服务器
[root@test ~]# cat /etc/ansible/hosts
[zabbix]
192.168.1.7
[root@test ansible_playbook]# pwd
/etc/ansible/ansible_playbook
[root@test ansible_playbook]# cat base.yaml
- hosts: zabbix
gather_facts: no
tasks:
- name: Disable Selinux
lineinfile:
path: /etc/selinux/config
regexp: "SELINUX=enforcing"
line: "SELINUX=disabled"
state: present
- name: Stop Selinux
shell: setenforce 0
- name: Open firewall port for http
firewalld: service=http permanent=true immediate=true state=enabled
- name: Create Epel Repo
get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
- name: copy zabbix.repo
copy: src=./conf/zabbix.repo dest=/etc/yum.repos.d/zabbix.repo
准备相关配置文件: selinux的配置文件,zabbix.repo文件(gpgcheck手动改0,不检查)
[root@test conf]# cat selinux.config
SELINUX=disabled
SELINUXTYPE=targeted
[root@ftp-server conf]# pwd
/etc/ansible/ansible_playbook/conf
[root@test conf]# cat zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=0
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0
2.安装http服务
[root@test ansible_playbook]# cat apache.yaml
- hosts: zabbix
tasks:
- name: Install Httpd
yum: name=httpd state=installed
- name: Start Http Service
service: name=httpd state=started enabled=yes
handlers:
- name: Rstart Http Service
service: name=httpd state=restarted
3.安装zabbix,并更改配置文件
[root@test ansible_playbook]# cat zabbix.yaml
- hosts: zabbix
tasks:
- name: Install zabbix-server-mysql
yum: name=zabbix-server-mysql state=installed
- name: Install zabbix-web-mysql
yum: name=zabbix-web-mysql state=installed
- name: Change zabbix DBPassword
shell: sed -i "s/# DBPassword=/DBPassword=P@ssw0rd/g" /etc/zabbix/zabbix_server.conf
- name: Change PHP timezone
shell: sed -i "s/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/g" /etc/httpd/conf.d/zabbix.conf
4.安装数据库mariadb,初始化数据库
[root@test ansible_playbook]# cat mariadb.yaml
- hosts: zabbix
tasks:
- name: install mariadb
yum: name=mariadb-devel,mariadb-server state=installed
- name: statr mariadb
service: name=mariadb state=started enabled=yes
- name: copy Script
copy: src=./conf/mariadb.sh dest=~/ mode=0770
- name: run Script
shell: ~/mariadb.sh
准备数据库初始化shell脚本,脚本有执行权限
[root@test conf]# cat mariadb.sh
#!/bin/bash
#创建数据库
a=`mysql -e "show databases;" | grep "zabbix"`
if [[ $a != "zabbix" ]];then
mysql -e "create database zabbix character set utf8 collate utf8_bin;"
fi
#创建用户
b=`mysql -e "select user,host from mysql.user;" | grep -o "zabbix"`
if [[ $b != "zabbix" ]];then
mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'P@ssw0rd';"
fi
#导入数据库
mysql -e "show tables from zabbix;" &> /dev/null
if [[ $? -eq 0 ]];then
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pP@ssw0rd zabbix
fi
5.重启服务
[root@test ansible_playbook]# cat restart.yaml
- hosts: zabbix
tasks:
- name: restatr mariadb
service: name=mariadb state=restarted enabled=yes
- name: restatr zabbix
service: name=zabbix-server state=restarted enabled=yes
- name: restatr zabbix-agent
service: name=zabbix-agent state=restarted enabled=yes
- name: Rstart Http Service
service: name=httpd state=restarted
6.主文件编写
[root@test ansible_playbook]# ls
apache.yaml base.yaml conf main.yaml mariadb.yaml restart.yaml zabbix.yaml
[root@test ansible_playbook]# cat main.yaml
- import_playbook: base.yaml
- import_playbook: apache.yaml
- import_playbook: zabbix.yaml
- import_playbook: mariadb.yaml
- import_playbook: restart.yam
[root@ftp-server ansible_playbook]# ansible-playbook --syntax-check main.yaml
playbook: main.yaml
PLAY [zabbix] **********************************
TASK [Disable Selinux] ***************************************
changed: [192.168.1.7]
TASK [Stop Selinux] ***************************************************************************************************
changed: [192.168.1.7]
TASK [Open firewall port for http] **********************************************************************************
changed: [192.168.1.7]
TASK [Create aliyun CentOS-Base Repo] ******************************************************************************************
changed: [192.168.1.7]
TASK [Create Epel Repo] *************************************************************************************
ok: [192.168.1.7]
TASK [copy zabbix.repo] ***********************************************************************************************************
ok: [192.168.1.7]
PLAY [zabbix] *********************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************
ok: [192.168.1.7]
TASK [Install zabbix-server-mysql] ******************************************************************************************************************
changed: [192.168.1.7]
TASK [Install zabbix-agent] ********************************************************************************************************************
changed: [192.168.1.7]
TASK [Install Zabbix frontend centos-release-scl] ***************************************************************************************************
changed: [192.168.1.7]
TASK [Install Zabbix frontend zabbix-web-mysql-scl] ***************************************************************************************************************
changed: [192.168.1.7]
TASK [Install Zabbix frontend zabbix-nginx-conf-scl] *******************************************************************************************************
changed: [192.168.1.7]
TASK [Change zabbix DBPassword] ************************************************************************************************************************************************
changed: [192.168.1.7]
TASK [Change zabbix nginx_config Port 80] ***********************************************************************************************************************************************
changed: [192.168.1.7]
TASK [Change zabbix nginx_config Server_name] *********************************************************************************************************************************************
changed: [192.168.1.7]
TASK [Change PHP User] ******************************************************************************************************************************************************************
changed: [192.168.1.7]
TASK [Change PHP timezone] ***************************************************************************************************************************************************************
changed: [192.168.1.7]
TASK [copy zabbix Font] *******************************************************************************************************************************************************************
changed: [192.168.1.7]
TASK [Change PHP zabbix font] ***************************************************************************************************************************************************
[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.
changed: [192.168.1.7]
PLAY [zabbix] **********************************************************************************************************************
TASK [install mariadb] ***********************************************************************************************************************************
changed: [192.168.1.7]
TASK [statr mariadb] ***************************************************************************************************************************************************
changed: [192.168.1.7]
TASK [copy Script] **************************************************************************************************************************************
changed: [192.168.1.7]
TASK [run Script] *********************************************************************************************************************
changed: [192.168.1.7]
RUNNING HANDLER [restart zabbix-agent service] *************************************************************************************************
changed: [192.168.1.7]
RUNNING HANDLER [restart zabbix-server service] ******************************************************************************************************************************
changed: [192.168.1.7]
RUNNING HANDLER [restart rh-nginx116-nginx service] ***********************************************************************************************
changed: [192.168.1.7]
RUNNING HANDLER [restart rh-php72-php-fpm service] ****************************************************************************************************************************
changed: [192.168.1.7]
PLAY RECAP **************************************************************************************************************************************************
[192.168.1.7] : ok=27 changed=24 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ftp-server ansible_playbook]#
``