修改hosts
vim /etc/ansible/hosts[webservers]
192.168.242.67[dbservers]
192.168.242.68[phpservers]
192.168.242.69
创建剧本文件
vim lnmp.yaml
- name: nginx playhosts: webserversremote_user: rootvars:- http_port: 192.168.242.67:80- http_hostname: www.ggl.com- root_dir: /usr/share/nginx/html- http_remote: 192.168.242.69:9000- service: nginxtasks:- name: disable firewalldservice: name=firewalld state=stopped enabled=no- name: disable selinuxshell: "/usr/sbin/setenforce 0"ignore_errors: true- name: copy nginx.repocopy: src=nginx.repo dest=/etc/yum.repos.d/- name: install nginxyum: name={{service}} state=latest- name: copy index.phpcopy: src=index.php dest={{root_dir}}- name: copy nginx.conftemplate: src=default.conf.j2 dest=/etc/nginx/conf.d/default.confnotify: reload nginx- name: start nginxservice: name={{service}} state=started enabled=yeshandlers:- name: reload nginxservice: name={{service}} state=reloaded- name: mysql playhosts: dbserversremote_user: roottasks:- name: disable firewalldservice: name=firewalld state=stopped enabled=no- name: disable selinuxshell: "/usr/sbin/setenforce 0"ignore_errors: true- name: yum remove mariadbyum: name=mariadb* state=absent- name: install rpmshell: wget -P /etc/yum.repos.d/ http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpmignore_errors: true- name: install noarch.rpmshell: yum -y install /etc/yum.repos.d/mysql57-community-release-el7-10.noarch.rpmignore_errors: true- name: replace wprdreplace: path=/etc/yum.repos.d/mysql-community.repo regexp='gpgcheck=1' replace='gpgcheck=0'ignore_errors: true- name: install mysqlyum: name="mysql-server"- name: start mysqldservice: name=mysqld state=started enabled=yes- name: login mysqlshell: mysql -uroot -p"$(grep "password" /var/log/mysqld.log | awk 'NR==1 {print $NF}')" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"- name: grantmysqlshell: mysql -uroot -pAdmin@123 -e "grant all privileges on *.* to root@'%' identified by 'Admin@123' with grant option;"- name: php playhosts: phpserversremote_user: roottasks:vars: - service: php-fpm- timezone: Asia/Shanghai- user_name: php- http_port: 192.168.242.69:9000- remote_addr: 192.168.242.67- root_dir: /usr/share/nginx/html- mysql_dir: /var/lib/mysql/mysql.socktasks:- name: disable firewalldservice: name=firewalld state=stopped enabled=no- name: disable selinuxshell: "/usr/sbin/setenforce 0"ignore_errors: true- 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: create php groupgroup: name={{user_name}}- name: create web dirfile: name={{root_dir}} state=directory- name: copy index.phpcopy: src=index.php dest={{root_dir}}- name: modify php.initemplate: src=php.ini.j2 dest=/etc/php.ininotify: reload php- name: copy www.confcopy: src=www.conf dest=/etc/php-fpm.d/www.conf- name: start phpservice: name={{service}} state=started enabled=yeshandlers:- name: reload phpservice: name={{service}} state=reloaded
##在剧本的当前目录中,需要准备需要的文件index.php
nginx.repo
default.conf.j2www.conf
php.ini.j2
### index.php<?php
phpinfo();
?>
# 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
###修改www.conf文件##主要修改几行配置文件user = php
group = php
listen = 192.168.242.69:9000
listen.allowed_clients = 192.168.242.67
## php.ini.j2 配置文件模板###修改几行1097行
mysqli.default_socket = {{mysql_dir}}877行
date.timezone = {{timezone}}
##templates目录 中存放 nginx 的 j2 的配置文件模板##修改几行listen {{http_port}};
server_name {{http_hostname}};root {{root_dir}};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;}