Ansible+LAMP+Zabbix
Server1:172.25.254.1
Server2:172.25.254.2
安装ansible
ansible-2.7.8-1.el7.noarch.rpm
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm
server1和server2配置免密:
[root@server1 ansible]# ssh-keygen
[root@server1 ansible]# ssh-copy-id server1
[root@server1 ansible]# ssh-copy-id server2
vim /etc/ansible/hosts
[web]
server1
server2
[db]
server2
配置ansible普通用户
ansible all -m user -a "name=syf password=westos"
Role-httpd:
[root@server1 roles]# mkdir httpd
[root@server1 roles]# cd httpd/
[root@server1 httpd]#
mkdir {files,templates,tasks,handlers,vars,meta,defaults}
[root@server1 httpd]# ls
defaults files handlers meta tasks templates vars
vim tasks/main.yml
- name: install httpd
yum: name=httpd state=present
- name: config httpd
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started
vim handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
将httpd配置文件模版复制到files/下
修改监听端口: Listen 172.25.254.2:80
Role-mariadb:
[root@server1 roles]# mkdir mariadb
[root@server1 roles]# cd mariadb/
[root@server1 mariadb]#
mkdir {files,templates,tasks,handlers,vars,meta,defaults}
[root@server1 mariadb]# ls
defaults files handlers meta tasks templates vars
vim tasks/main.yml
- name: install mariadb server
yum: name={{ item }} state=present
with_items:
- mariadb-server
- mariadb-devel
- MySQL-python
- name: config mariadb
copy: src=my.cnf dest=/etc/my.cnf
notify: restart mariadb
- name: start mariadb server
service: name=mariadb state=started
vim handlers/main.yml
- name: restart mariadb
service: name=mariadb state=restarted
cp /etc/my.cnf /etc/ansible/roles/mariadb/files
添加:character-set-server=utf8
Role-php:
[root@server1 roles]# mkdir php
[root@server1 roles]# cd php/
[root@server1 php]# mkdir {files,templates,tasks,handlers,vars,meta,defaults}
[root@server1 php]# ls
defaults files handlers meta tasks templates vars
vim tasks/main.yml
- name: install php
yum: name={{ item }} state=present
with_items:
- php
notify: restart httpd
vim handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
Role-zabbix
[root@server1 roles]# mkdir zabbix
[root@server1 roles]# cd zabbix/
[root@server1 zabbix]#
mkdir {files,templates,tasks,handlers,vars,meta,defaults}
[root@server1 zabbix]# ls
defaults files handlers meta tasks templates vars
vim tasks/main.yml
- name: copy zabbix.repo
copy: src=zabbix.repo dest=/etc/yum.repos.d/zabbix.repo
- name: install zabbix-server
yum: name=zabbix-server,zabbix-agent state=present
notify: "init zabbix db"
- name: config zabbix server
copy: src=zabbix_server.conf dest=/etc/zabbix/zabbix_server.conf
notify: restart zabbix server
- name: start zabbix server
service: name={{ item }} state=started
with_items:
- zabbix-server
- zabbix-agent
vim handlers/main.yml
- name: create database
mysql_db: name=zabbix state=present
listen: "init zabbix db"
- name: create zabbix user
mysql_user: name=zabbix password=zabbix priv=zabbix.*:ALL state=present
listen: "init zabbix db"
- name: import create.sql.gz
mysql_db: name=zabbix
state=import target=/usr/share/doc/zabbix-server-mysql-4.0.5/create.sql.gz
listen: "init zabbix db"
- name: restart zabbix server
service: name=zabbix-server state=restarted
vim files/zabbix.repo
[zabbix]
name=zabbix4.0
baseurl=file:///root/4.0
gpgcheck=0
将zabbix_server.conf文件复制到files/
vim files/zabbix_server.conf
DBPassord=zabbix
vim /etc/ansible/lamp.yml
---
- hosts: server2
roles:
- mariadb
- zabbix
- httpd
- php
[root@server1 ansible]# ansible-playbook lamp.yml
[root@server1 ~]# vim index.html
[root@server1 ~]# ansible server2 -u syf -b -m copy -a "src=index.html dest=/var/www/html/index.html"
server2 | CHANGED => {
"changed": true,
"checksum": "b097c51bbb5bac74fdaeca37e340e8132f0e4c69",
"dest": "/var/www/html/index.html",
"gid": 0,
"group": "root",
"md5sum": "f49bb5c10abb2f48802ed1d00a8feebc",
"mode": "0644",
"owner": "root",
"size": 15,
"src": "/home/syf/.ansible/tmp/ansible-tmp-1553494549.84-176136904469032/source",
"state": "file",
"uid": 0
}
[root@server1 ~]# vim index.php
[root@server1 ~]# ansible server2 -u syf -b -m copy -a "src=index.php dest=/var/www/html/index.php"
server2 | CHANGED => {
"changed": true,
"checksum": "26af88945e23289d15e128606a29932b3d78787c",
"dest": "/var/www/html/index.php",
"gid": 0,
"group": "root",
"md5sum": "62210a938d0199092c2d3976a45bf86d",
"mode": "0644",
"owner": "root",
"size": 22,
"src": "/home/syf/.ansible/tmp/ansible-tmp-1553494588.69-144347487690584/source",
"state": "file",
"uid": 0
}
标签:Zabbix,httpd,name,LAMP,server1,zabbix,Ansible,server,root From: https://blog.51cto.com/u_15883840/5870328