使用Roles模块搭建LNMP架构
- 1.Ansible-playbook中部署Nginx角色
- 2.Ansible-playbook中部署PHP角色
- 3.Ansible-playbook中部署MySQL角色
- 4.启动安装分布式LNMP
1.Ansible-playbook中部署Nginx角色
创建nginx角色所需要的工作目录;
mkdir -p /etc/ansible/playbook/roles/nginx
mkdir -p /etc/ansible/playbook/roles/nginx/defaults
mkdir -p /etc/ansible/playbook/roles/nginx/files
mkdir -p /etc/ansible/playbook/roles/nginx/handlers
mkdir -p /etc/ansible/playbook/roles/nginx/meta
mkdir -p /etc/ansible/playbook/roles/nginx/tasks
mkdir -p /etc/ansible/playbook/roles/nginx/templates
mkdir -p /etc/ansible/playbook/roles/nginx/vars
制作/etc/ansible/playbook/roles/nginx/files/index.php网页目录;
vim /etc/ansible/playbook/roles/nginx/files/index.php
<?php
phpinfo();
?>
制作/etc/ansible/playbook/roles/nginx/files/nginx.repo源;
vim /etc/ansible/playbook/roles/nginx/files/nginx.repo
# nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
制作/etc/ansible/playbook/roles/nginx/handlers/main.yml文件,加载nginx的配置模板文件;
vim /etc/ansible/playbook/roles/nginx/handlers/main.yml
- name: reload nginx
service: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/nginx/tasks/init.yml文件;
vim /etc/ansible/playbook/roles/nginx/tasks/init.yml
- name: disable firewalld
service: name=firewalld state=stopped enabled=no
ignore_errors: True
- name: disable selinux
shell: "/usr/sbin/setenforce 0"
ignore_errors: true
制作/etc/ansible/playbook/roles/nginx/tasks/main.yml文件;
- include: "init.yml"
- name: copy nginx yum repo file
copy: src=nginx.repo dest=/etc/yum.repos.d/
- name: install nginx
yum: name={{pkg}} state=latest
- name: copy index.php
copy: src=index.php dest={{root_dir}}
- name: copy nginx template configuration file
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
notify: reload nginx
- name: start nginx
service: name={{service}} state=started enabled=yes
制作/etc/ansible/playbook/roles/nginx/templates/default.conf.j2模板文件;
vim /etc/ansible/playbook/roles/nginx/templates/default.conf.j2
---修改2到8行-----------
listen {{http_port}};
server_name {{http_hostname}};
#access_log /var/log/nginx/host.access.log main;
location / {
root {{root_dir}};
---修改29到36行-----------
location ~ \.php$ {
root {{root_dir}};
fastcgi_pass {{http_remote}};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME {{root_dir}}$fastcgi_script_name;
include fastcgi_params;
}
制作/etc/ansible/playbook/roles/nginx/vars/main.yml文件;
http_port: 192.168.80.50:80
http_hostname: www.gzy.com
root_dir: /usr/share/nginx/html
http_remote: 192.168.80.80:9000
pkg: nginx
service: nginx
2.Ansible-playbook中部署PHP角色
创建php角色所需要的工作目录;
mkdir -p /etc/ansible/playbook/roles/php
mkdir -p /etc/ansible/playbook/roles/php/defaults
mkdir -p /etc/ansible/playbook/roles/php/files
mkdir -p /etc/ansible/playbook/roles/php/handlers
mkdir -p /etc/ansible/playbook/roles/php/meta
mkdir -p /etc/ansible/playbook/roles/php/tasks
mkdir -p /etc/ansible/playbook/roles/php/templates
mkdir -p /etc/ansible/playbook/roles/php/vars
制作/etc/ansible/playbook/roles/php/files/index.php的网页文件;
vim /etc/ansible/playbook/roles/php/files/index.php
<?php
phpinfo();
?>
制作/etc/ansible/playbook/roles/php/handlers/main.yml文件;
vim /etc/ansible/playbook/roles/php/handlers/main.yml
- name: reload php
service: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/php/tasks/main.yml文件;
/etc/ansible/playbook/roles/php/tasks]# vim main.yml
- name: install yum repo
shell: "rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm"
ignore_errors: True
- name: install php
with_items:
- php72w
- php72w-cli
- php72w-common
- php72w-devel
- php72w-embedded
- php72w-gd
- php72w-mbstring
- php72w-pdo
- php72w-xml
- php72w-fpm
- php72w-mysqlnd
- php72w-opcache
yum: name={{item}}
- name: create php user
user: name={{user_name}}
- name: crate web root dir
file: name={{root_dir}} state=directory
- name: copy index.php
copy: src=index.php dest={{root_dir}}
- name: modify php configuration file
replace: path=/etc/php.ini regexp=";date.timezone =" replace="date.timezone = Asia/Shanghai"
notify: reload php
- name: modify username and groupname in www.conf
replace: path=/etc/php-fpm.d/www.conf regexp="apache" replace="{{user_name}}"
notify: reload php
- name: modify listen addr in www.conf
replace: path=/etc/php-fpm.d/www.conf regexp="127.0.0.1:9000" replace="{{http_port}}"
notify: reload php
- name: modify allowed client in www.conf
replace: path=/etc/php-fpm.d/www.conf regexp="127.0.0.1" replace="{{remote_addr}}"
notify: reload php
- name: start php
service: name={{service}} state=started enabled=yes
制作/etc/ansible/playbook/roles/php/vars/main.yml文件;
vim /etc/ansible/playbook/roles/php/vars/main.yml
timezone: Asia/Shanghai
user_name: php
http_port: 192.168.80.80:9000
remote_addr: 192.168.80.50
root_dir: /usr/share/nginx/html
service: php-fpm
3.Ansible-playbook中部署MySQL角色
创建mysql角色所需要的工作目录;
mkdir -p /etc/ansible/playbook/roles/mysql
mkdir -p /etc/ansible/playbook/roles/mysql/defaults
mkdir -p /etc/ansible/playbook/roles/mysql/files
mkdir -p /etc/ansible/playbook/roles/mysql/handlers
mkdir -p /etc/ansible/playbook/roles/mysql/meta
mkdir -p /etc/ansible/playbook/roles/mysql/tasks
mkdir -p /etc/ansible/playbook/roles/mysql/templates
mkdir -p /etc/ansible/playbook/roles/mysql/vars
制作/etc/ansible/playbook/roles/mysql/handlers/main.yml文件;
vim /etc/ansible/playbook/roles/mysql/handlers/main.yml
-
name: reload mysql
service: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/mysql/tasks/main.yml文件;
vim /etc/ansible/playbook/roles/mysql/tasks/main.yml
- name: remove mariadb
shell: yum remove mariadb* -y
ignore_errors: True
- name: install yum repo
shell: "wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm && rpm -ivh mysql57-community-release-el7-11.noarch.rpm"
ignore_errors: True
- name: modify gpgcheck
replace: path=/etc/yum.repos.d/mysql-community.repo regexp="gpgcheck=1" replace="gpgcheck=0"
- name: install mysql
yum: name=mysql-server state=latest
- name: start mysql
service: name={{service}} state=started enabled=yes
- name: make passwd.sh
script: /etc/ansible/playbook/roles/mysql/files/passwd.sh
制作/etc/ansible/playbook/roles/mysql/vars/main.yml文件;
vim /etc/ansible/playbook/roles/mysql/vars/main.yml
service: mysqld.service
4.启动安装分布式LNMP
vim /etc/ansible/playbook/lnmp.yml
- name: nginx play
hosts: webservers
remote_user: root
roles:
- nginx
- name: php play
hosts: phpservers
remote_user: root
roles:
- php
- name: mysql play
hosts: mysqlservers
remote_user: root
roles:
- mysql