一、目录结构
ansible/
├── playbooks/
│ ├── deploy_lnmp.yml
│ └── roles/
│ ├── nginx/
│ │ ├── tasks/
│ │ │ └── main.yml
│ │ └── templates/
│ │ │ └── nginx.conf.j2
└── handlers/
└── main.yml
│ ├── mysql/
│ │ ├── tasks/
│ │ │ └── main.yml
│ │ └── vars/
│ │ └── main.yml
│ └── php/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── php.ini.j2
playbooks/ 自定义主功能目录,主yml文件存放位置
roles/ 下面定义一系列角色 nginx,mysql,php都可被视为一个角色
每个角色内可根据需要创建目录 常用目录结构:
tasks/ 这个文件内可编写与 MySQL 相关的任务,例如安装,创用户创库。
templates/ 模板文件存放目录,例如用于生成nginx.conf.j2的配置文件。Jinja2 是 Ansible 使用的模板引擎,可以动态生成配置文件内容。
vars/ 这个目录下可以存放变量 但优先级较低
handlers/
二、源码
deploy_lnmp.yml
---
- name : 配置ailiyum 源
hosts : test_playbook
remote_user : root
tasks :
- name : Configure Aliyun YUM repository
yum_repository :
name : aliyun
description : Aliyun YUM Repository
baseurl : http: //mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck : 1
gpgkey : http: //mirrors.aliyun.com/centos/RPM- GPG- KEY- CentOS- 7
enabled : yes
- name : 安装 EPEL 仓库
yum :
name : epel- release
state : present
- name : 部署 LNMP 堆栈
hosts : test_playbook
become : yes
vars :
mysql_root_password : "123456"
mysql_database : "mydb"
mysql_user : "myuser"
mysql_user_password : "123456"
server_name : "example.com"
document_root : "/usr/share/nginx/html"
roles :
- nginx
- mysql
- php
mysql 下的main.yml
---
- name : 安装 MySQL- python
yum :
name : MySQL- python
state : present
become : yes
- name : 安装 MariaDB
yum :
name : mariadb- server
state : present
become : yes
- name : 启动 MariaDB 服务
systemd :
name : mariadb
state : started
enabled : yes
become : yes
- name : 配置 MariaDB root 用户密码
mysql_user :
login_user : root
login_password : "{{ mysql_root_password }}"
name : root
password : "{{ mysql_root_password }}"
host_all : yes
state : present
become : yes
- name : 创建数据库
mysql_db :
name : "{{ mysql_database }}"
state : present
become : yes
- name : 创建 MySQL 用户
mysql_user :
name : "{{ mysql_user }}"
password : "{{ mysql_user_password }}"
priv : "*.*:ALL"
state : present
become : yes
- name : 配置 MySQL 用户权限
mysql_user :
name : "{{ mysql_user }}"
password : "{{ mysql_user_password }}"
priv : "{{ mysql_database }}.*:ALL"
host_all : yes
state : present
become : yes
nginx 下的main.yml
---
- name : 安装 Nginx
yum :
name : nginx
state : present
- name : 启动并启用 Nginx 服务
systemd :
name : nginx
state : started
enabled : yes
- name : 配置 Nginx 虚拟主机
template :
src : nginx_vhost.conf.j2
dest : /etc/nginx/conf.d/{ { server_name } } .conf
notify :
- 重启 Nginx
nginx_vhost.conf.j2
server {
listen 80;
server_name { { server_name } } ;
root { { document_root } } ;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1: 9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
handlers 下的main
---
- name : 重启 Nginx
systemd :
name : nginx
state : restarted
php下的main
---
- name : 安装 PHP 及相关扩展
yum :
name :
- php
- php- fpm
- php- mysql
state : present
- name : 启动并启用 PHP- FPM 服务
systemd :
name : php- fpm
state : started
enabled : yes
[ root@localhost handlers ]
三、查看验证
[ root@localhost ~]
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \ g.
Your MariaDB connection id is 11
Server version: 5.5 .68-MariaDB MariaDB Server
Copyright ( c) 2000 , 2018 , Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [ ( none) ] > show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| test |
| test_db |
+--------------------+
4 rows in set ( 0.00 sec)
MariaDB [ ( none) ] >
浏览器访问虚拟机IP地址 验证nginx 服务是否正常