- hosts: webserver,database
remote_user: root
gather_facts: no
vars:
- NGINX_USER: nginx
- NGINX_PORT: 80
- NGINX_FORKS: 2
- BASE_DIR: "/usr/share/nginx/wordpress"
- MYSQL_DB: wordpress
- MYSQL_USER: wordpress
- MYSQL_PASSWD: "Xs123456"
- MYSQL_HOST: "%"
tasks:
- name: "step1: 安装数据库"
yum: name=mariadb-server,mariadb state=present
#service: name=mariadb state=started enabled=yes
when: name == 'db'
tags: install_db
- name: "step2: 安装php和nginx的源"
yum: name={{ item }}
with_items:
- epel-release
- http://rpms.remirepo.net/enterprise/remi-release-7.rpm
when: name == 'web'
tags: install_yumrepo
- name: "step3: 安装php8和nginx"
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,nginx state=present
when: name == 'web'
tags: install_php_nginx
- name: "step4: 启动数据库"
service: name=mariadb state=started enabled=yes
when: name == 'db'
tags: start_db
- name: "step5: 配置nginx"
template: src=./nginx.conf dest=/etc/nginx/nginx.conf
when: name == 'web'
tags: nginx_conf
- name: "step6: 启动PHP和nginx"
service: name={{ item }} state=started
with_items:
- php80-php-fpm
- nginx
when: name == 'web'
tags: start_php_nginx
- name: "step7: 创建数据库"
shell: mysql -e "create database if not exists {{ MYSQL_DB }}; grant all on {{ MYSQL_DB }}.* to '{{ MYSQL_USER }}'@'{{ MYSQL_HOST }}' identified by '{{ MYSQL_PASSWD }}'; flush privileges"
when: name == 'db'
tags: create_db
- name: "step8: 上传wordpress代码"
copy: src=/opt/wordpress dest=/usr/share/nginx mode=777
when: name == 'web'
tags: push_code