node1:192.168.91.139:zabbix-server node2:192.168.91.137:ansible
在node2:
实现各主机间ssh无密钥通信 [root@localhost ~]#ssh-key [root@localhost ~]#ssh-copy-id root@192.168.91.137 [root@localhost ~]#ls .ssh/ authorized_keys id_rsa id_rsa.pub known_hosts [root@localhost ~]#scp -r .ssh/* 192.168.91.139:~/.ssh/ 安装ansible [root@localhost ~]#yum install -y ansible 创建主机清单 [root@localhost ~]#vim /etc/ansible/hosts [zabbixsrv] 192.168.91.139 测试 [root@localhost ~]#ansible zabbixsrv -m ping 192.168.91.139 | SUCCESS => { "changed": false, "ping": "pong" } 生成roles文件 [root@localhost ~]#mkdir /etc/ansible/roles/zabbixsrv/{files,templates,tasks,handlers,vars,defaults,meta} -pv [root@localhost ~]#tree /etc/ansible/ /etc/ansible/ ├── ansible.cfg ├── hosts └── roles └── zabbixsrv ├── defaults ├── files ├── handlers ├── meta ├── tasks ├── templates └── vars [root@localhost /etc/ansible]#vim ~/zabbixsrv.sh #!/bin/bash rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm #安装官方仓库 yum clean all yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent 提前准备zabbix_server.conf,修改配置并放到/etc/ansible/roles/zabbixsrv/files/下 DBHost=192.168.91.139 DBName=zabbix DBUser=zabbix DBPassword=122333 [root@localhost /etc/ansible]#vim roles/zabbixsrv/tasks/main.yml - name: copy script copy: src=~/zabbixsrv.sh dest=~/ mode=0700 - name: run script shell: ~/zabbixsrv.sh - name: config zabbix copy: src=/etc/ansible/roles/zabbixsrv/files/zabbix_server.conf dest=/etc/zabbix/ - name: start service service: name=zabbix-server state=started [root@localhost /etc/ansible]#mkdir roles/mariadb/{files,tasks} -pv 提前准备my.cnf,放到/etc/ansible/roles/mariadb/files/ innodb_file_per_table=on skip_name_resolve=on log_bin=bin-log [root@localhost /etc/ansible]#vim ~/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 on zabbix.* to zabbix@'192.168.91.139' identified by '122333';" 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 -p122333 -h192.168.91.139 zabbix fi [root@localhost /etc/ansible]#vim roles/mariadb/tasks/main.yml - name: install mariadb-server yum: name=mariadb-server state=installed - name: config mariadb copy: src=/etc/ansible/roles/mariadb/files/my.cnf dest=/etc/my.cnf - name: start mariadb service: name=mariadb state=started - name: copy script copy: src=~/mariadb.sh dest=~/ mode=0770 - name: run script shell: ~/mariadb.sh [root@localhost /etc/ansible]#mkdir roles/ap/{files,tasks} -pv 提前准备zabbix.conf,放到/etc/ansible/roles/ap/files/下 php_value date.timezone Asia/Shanghai [root@localhost /etc/ansible]#vim roles/ap/tasks/main.yml - name: install paskages yum: name={{ item }} with_items: - httpd - php - php-mysql - name: config php copy: src=/etc/ansible/roles/ap/files/zabbix.conf dest=/etc/httpd/conf.d/ - name: start service service: name=httpd state=started [root@localhost /etc/ansible]#vim all.yml - hosts: zabbixsrv remote_user: root roles: - zabbixsrv - mariadb - ap [root@localhost /etc/ansible]#ansible-playbook all.yml 登陆zabbix 192.168.91.139/zabbix 账号admin 密码zabbix