准备环境:
1.需要三台服务器:主机服务器((10.36.192.129)、db服务器(10.36.192.131),web服务器(10.36.192.130),剧本都是写在主机服务器上。
2.主机服务器需要做ansible解析
vim /etc/ansible/hosts
[web]
10.36.192.130
[db]
10.36.192.131
3.这里的yum源需要自己配置,我这里是有一个yum源的服务器
一:剧本安装数据库
---
- hosts: db
remote_user: root
vars:
- db_passwd: 'QianFeng@123'
- db_name: 'wordpress'
tasks:
- name: 下载yum源
shell: curl -o /opt/yum-server.sh http://10.36.192.100/yum-server.sh
- name: 安装yum源
shell: sh /opt/yum-server.sh
- name: 安装数据库
yum: name=mariadb-server,mariadb state=present disablerepo=mysql-5.7-community
- name: 启动数据库
service: name=mariadb state=started enabled=true
- name: 配置数据库
shell: mysql -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_passwd }}'; flush privileges"
二.剧本安装nginx,php,及上线wordpress
hosts: web
remote_user: root
vars:
- nginx_user: nginx
- nginx_forks: auto
- nginx_port: 80
tasks:
- name: 下载yum源
shell: curl -o /opt/yum-server.sh http://10.36.192.100/yum-server.sh
- name: 安装yum源
shell: sh /opt/yum-server.sh
- name: 安装nginx
yum: name=nginx state=present
- name: nginx配置文件
template: src=/root/nginx.conf dest=/etc/nginx/
- name: 启动Nginx
service: name=nginx state=started enabled=true
- name: 安装PHP
yum: name=php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm state=present
- name: 启动PHP
service: name=php80-php-fpm state=started enabled=true
- name: 拷贝wordpress源代码
unarchive: src=/root/wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html