使用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 nginxservice: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/nginx/tasks/init.yml文件;
vim /etc/ansible/playbook/roles/nginx/tasks/init.yml
- name: disable firewalldservice: name=firewalld state=stopped enabled=noignore_errors: True- name: disable selinuxshell: "/usr/sbin/setenforce 0"ignore_errors: true
制作/etc/ansible/playbook/roles/nginx/tasks/main.yml文件;
- include: "init.yml"- name: copy nginx yum repo filecopy: src=nginx.repo dest=/etc/yum.repos.d/- name: install nginxyum: name={{pkg}} state=latest- name: copy index.phpcopy: src=index.php dest={{root_dir}}- name: copy nginx template configuration filetemplate: src=default.conf.j2 dest=/etc/nginx/conf.d/default.confnotify: reload nginx- name: start nginxservice: 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 phpservice: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/php/tasks/main.yml文件;
/etc/ansible/playbook/roles/php/tasks]# vim main.yml - name: install yum reposhell: "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 phpwith_items:- php72w- php72w-cli- php72w-common- php72w-devel- php72w-embedded- php72w-gd- php72w-mbstring- php72w-pdo- php72w-xml- php72w-fpm- php72w-mysqlnd- php72w-opcacheyum: name={{item}}- name: create php useruser: name={{user_name}}- name: crate web root dirfile: name={{root_dir}} state=directory- name: copy index.phpcopy: src=index.php dest={{root_dir}}- name: modify php configuration filereplace: path=/etc/php.ini regexp=";date.timezone =" replace="date.timezone = Asia/Shanghai"notify: reload php- name: modify username and groupname in www.confreplace: path=/etc/php-fpm.d/www.conf regexp="apache" replace="{{user_name}}"notify: reload php- name: modify listen addr in www.confreplace: 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.confreplace: path=/etc/php-fpm.d/www.conf regexp="127.0.0.1" replace="{{remote_addr}}"notify: reload php- name: start phpservice: 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 mysqlservice: name={{service}} state=reloaded
制作/etc/ansible/playbook/roles/mysql/tasks/main.yml文件;
vim /etc/ansible/playbook/roles/mysql/tasks/main.yml - name: remove mariadbshell: yum remove mariadb* -yignore_errors: True- name: install yum reposhell: "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 gpgcheckreplace: path=/etc/yum.repos.d/mysql-community.repo regexp="gpgcheck=1" replace="gpgcheck=0"- name: install mysqlyum: name=mysql-server state=latest- name: start mysqlservice: name={{service}} state=started enabled=yes- name: make passwd.shscript: /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 playhosts: webserversremote_user: rootroles:- nginx- name: php playhosts: phpserversremote_user: rootroles:- php- name: mysql playhosts: mysqlserversremote_user: rootroles:- mysql