目录结构
[root@ansible ansible]# tree ./
./
├── ansible.cfg
├── hosts
├── playbook03292102.tar.gz
├── playbook.yml
└── roles
├── mariadb
│ ├── file
│ │ └── mariadb.tar.gz
│ ├── scripts
│ │ ├── passwd.sh
│ │ └── zabbixuser.sh
│ └── tasks
│ └── main.yml
├── nginx
│ ├── conf
│ │ ├── nginx.repo
│ │ └── w.conf
│ ├── file
│ │ └── nginx-1.16.1-1.el7.ngx.x86_64.rpm
│ ├── scripts
│ ├── tasks
│ │ └── main.yml
│ └── tools
├── php-fpm
│ ├── conf
│ │ └── www.conf
│ ├── file
│ │ └── php-fpm.tar.gz
│ ├── repo
│ │ ├── epel.repo
│ │ ├── epel-testing.repo
│ │ ├── webtatic-archive.repo
│ │ ├── webtatic.repo
│ │ └── webtatic-testing.repo
│ ├── scripts
│ ├── tasks
│ │ └── main.yml
│ └── tools
└── zabbix
├── conf
│ ├── zabbix.conf
│ └── zabbix_server.conf
├── file
│ ├── defines.inc.php
│ ├── simkai.ttf
│ ├── zabbix.4.2.8.tar.gz
│ └── zabbix-release-4.2-2.el7.noarch.rpm
├── opt
│ ├── mariadb-5.5.64-1.el7.x86_64.rpm
│ ├── mariadb-server-5.5.64-1.el7.x86_64.rpm
│ ├── perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
│ ├── perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
│ ├── perl-Data-Dumper-2.145-3.el7.x86_64.rpm
│ ├── perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
│ ├── perl-DBI-1.627-4.el7.x86_64.rpm
│ ├── perl-IO-Compress-2.061-2.el7.noarch.rpm
│ ├── perl-Net-Daemon-0.48-5.el7.noarch.rpm
│ └── perl-PlRPC-0.2020-14.el7.noarch.rpm
├── repo
│ └── zabbix.repo
├── scripts
├── tasks
│ └── main.yml
└── tools
[root@ansible ansible]# cat playbook.yml
- hosts: db
remote_user: root
roles:
- mariadb
- hosts: web
remote_user: root
roles:
- nginx
- php-fpm
- hosts: db
remote_user: root
roles:
- zabbix
[root@ansible ansible]# cat roles/mariadb/tasks/main.yml
- name: push mariadb.tar
copy: src=./file/mariadb.tar.gz dest=/root
- name: tar mariadb
shell: tar zxf mariadb.tar.gz -C /opt/
- name: install mariadb
shell: cd /opt && yum localinstall -y /opt/*.rpm
async: 0
- name: start mariadb
service: name=mariadb state=started enabled=yes
- name: copy sh
copy: src=scripts/passwd.sh dest=/root/
- name: mysql_secure_installation
shell: "chmod +x passwd.sh &&sh passwd.sh"
ignore_errors: yes
- name: rm -rf /opt/*
shell: rm -rf /opt/* /root/passwd.sh /root/mariadb.tar.gz
[root@ansible ansible]# cat roles/nginx/tasks/main.yml
- name: add repo
get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
- name: copy nginx.repo to repo
copy: src=./conf/nginx.repo dest=/etc/yum.repos.d/
- name: copy rpm to root
copy: src=./file/nginx-1.16.1-1.el7.ngx.x86_64.rpm dest=/opt
- name: install nginx
shell: yum localinstall -y /opt/nginx*.rpm
async: 0
- name: Configure nginx
shell: mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.off
- name: copy nginx.conf
copy: src=./conf/w.conf dest=/etc/nginx/conf.d/
- name: start nginx
service: name=nginx state=started enabled=yes
[root@ansible ansible]# cat roles/php-fpm/tasks/main.yml
- name: yumrepo
copy: src=./repo/ dest=/etc/yum.repos.d/
- name: phptar
copy: src=file/php-fpm.tar.gz dest=/opt/
- name: tar zcf
shell: tar -zxf /opt/php-fpm.tar.gz -C /opt/
- name: install php
shell: yum localinstall -y /opt/php-fpm/*.rpm
async: 0
- name: configure php
copy: src=conf/www.conf dest=/etc/php-fpm.d
- name: start php
service: name=php-fpm state=started enabled=yes
- name: rm -rf /opt/*
shell: rm -rf /opt/*
[root@ansible ansible]# cat roles/zabbix/tasks/main.yml
- name: add repo
get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
- name: clean
shell: yum clean all
- name: push tar and rpm
copy: src=./file/zabbix-release-4.2-2.el7.noarch.rpm dest=/opt/
- name: push tar and rpm
copy: src=./file/zabbix.4.2.8.tar.gz dest=/opt/
- name: rpm zabbixrepo
shell: "rpm -ivh /opt/zabbix-release-4.2-2.el7.noarch.rpm && rm -rf /opt/zabbix-release-4.2-2.el7.noarch.rpm"
poll: 0
ignore_errors: yes
- name: copy tar to opt
copy: src=./file/zabbix.4.2.8.tar.gz dest=/opt/
- name: tar zabbix
shell: cd /opt && tar -zxf /opt/zabbix.4.2.8.tar.gz && rm -rf /opt/zabbix.4.2.8.tar.gz
- name: install zabbix
shell: "yum -y localinstall /opt/zabbix*.rpm"
async: 0
- name: create zabbixdb and user
shell: mysql -p123qwe -e "create database zabbix;grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';"
ignore_errors: yes
- name: Configure zabbixdb
shell: zcat /usr/share/doc/zabbix-server-mysql-4.2.8/create.sql.gz | mysql zabbix -uzabbix -pzabbix
ignore_errors: yes
- name: configure zabbix.conf
copy: src=./conf/zabbix_server.conf dest=/etc/zabbix/zabbix_server.conf
- name: configure zabbix.conf
copy: src=./conf/zabbix.conf dest=/etc/httpd/conf.d/zabbix.conf
- name: start zabbix
service: name=httpd enabled=yes state=started
- name: start zabbix-server
service: name=zabbix-server enabled=yes state=started
- name: start zabbix
service: name=zabbix-agent enabled=yes state=started
- name: Fonts
copy: src=./file/simkai.ttf dest=/usr/share/zabbix/assets/fonts/
- name: Fonts to defines.inc.php
copy: src=./file/defines.inc.php dest=/usr/share/zabbix/include/
- name: chmod
file: path=/usr/share/zabbix/assets/fonts/simkai.ttf mode=777
- name: firewalld add server http
firewalld: service=http state=enabled permanent=yes immediate=yes
- name: firewalld add server mysql
firewalld: service=mysql state=enabled permanent=yes immediate=yes
- name: firewalld add server zabbix-agent
firewalld: service=zabbix-agent state=enabled permanent=yes immediate=yes
- name: firewalld add server zabbix-server
firewalld: service=zabbix-server state=enabled permanent=yes immediate=yes