使用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/nginxmkdir -p /etc/ansible/playbook/roles/nginx/defaultsmkdir -p /etc/ansible/playbook/roles/nginx/filesmkdir -p /etc/ansible/playbook/roles/nginx/handlersmkdir -p /etc/ansible/playbook/roles/nginx/metamkdir -p /etc/ansible/playbook/roles/nginx/tasksmkdir -p /etc/ansible/playbook/roles/nginx/templatesmkdir -p /etc/ansible/playbook/roles/nginx/vars
制作/etc/ansible/playbook/roles/nginx/files/index.php网页目录;
vim /etc/ansible/playbook/roles/nginx/files/index.php<" />();?>
制作/etc/ansible/playbook/roles/nginx/files/nginx.repo源;
vim /etc/ansible/playbook/roles/nginx/files/nginx.repo# nginx.repo[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_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=firewalldstate=stoppedenabled=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.repodest=/etc/yum.repos.d/- name: install nginxyum: name={{pkg}}state=latest- name: copy index.phpcopy: src=index.phpdest={{root_dir}}- name: copy nginx template configuration filetemplate: src=default.conf.j2dest=/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.logmain;location / { root {{root_dir}};---修改29到36行-----------location ~ \.php$ { root {{root_dir}}; fastcgi_pass {{http_remote}}; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME{{root_dir}}$fastcgi_script_name;includefastcgi_params; }
制作/etc/ansible/playbook/roles/nginx/vars/main.yml文件;
http_port: 192.168.80.50:80http_hostname: www.gzy.comroot_dir: /usr/share/nginx/htmlhttp_remote: 192.168.80.80:9000pkg: nginxservice: nginx
2.Ansible-playbook中部署PHP角色
创建php角色所需要的工作目录;
mkdir -p /etc/ansible/playbook/roles/phpmkdir -p /etc/ansible/playbook/roles/php/defaultsmkdir -p /etc/ansible/playbook/roles/php/filesmkdir -p /etc/ansible/playbook/roles/php/handlersmkdir -p /etc/ansible/playbook/roles/php/metamkdir -p /etc/ansible/playbook/roles/php/tasksmkdir -p /etc/ansible/playbook/roles/php/templatesmkdir -p /etc/ansible/playbook/roles/php/vars
制作/etc/ansible/playbook/roles/php/files/index.php的网页文件;
vim /etc/ansible/playbook/roles/php/files/index.php <" />();?>
制作/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.iniregexp=";date.timezone ="replace="date.timezone = Asia/Shanghai"notify: reload php- name: modify username and groupname in www.confreplace: path=/etc/php-fpm.d/www.confregexp="apache"replace="{{user_name}}"notify: reload php- name: modify listen addr in www.confreplace: path=/etc/php-fpm.d/www.confregexp="127.0.0.1:9000"replace="{{http_port}}"notify: reload php- name: modify allowed client in www.confreplace: path=/etc/php-fpm.d/www.confregexp="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/Shanghaiuser_name: phphttp_port: 192.168.80.80:9000remote_addr: 192.168.80.50root_dir: /usr/share/nginx/htmlservice: php-fpm
3.Ansible-playbook中部署MySQL角色
创建mysql角色所需要的工作目录;
mkdir -p /etc/ansible/playbook/roles/mysqlmkdir -p /etc/ansible/playbook/roles/mysql/defaultsmkdir -p /etc/ansible/playbook/roles/mysql/filesmkdir -p /etc/ansible/playbook/roles/mysql/handlersmkdir -p /etc/ansible/playbook/roles/mysql/metamkdir -p /etc/ansible/playbook/roles/mysql/tasksmkdir -p /etc/ansible/playbook/roles/mysql/templatesmkdir -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.reporegexp="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