阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ansible项目实战LNMP

333次阅读
没有评论

共计 5006 个字符,预计需要花费 13 分钟才能阅读完成。

通过 ansible roles 配置 lnmp 环境,nginx通过源码编译安装,php通过源码编译安装,MySQL通过 yum 安装(mysql源码编译超级慢 )支持系统(CentOS6.xcentos7.x系列)

说明: nginxphp源码包放到对应的角色文件下的 files 目录下,通过 vars/main.yml 控制安装的版本和路径。如下:

[root@ansible roles]# cat nginx/vars/main.yml
DOWNLOAD_DIR: "/usr/local/src/" #软件包拷贝到目标主机的存放路径
INSTALL_DIR: "/usr/local/" #安装路径
NGINX_VERSION: "1.12.2" #软件包版本
USER: "nginx" #运行的用户
GROUP: "nginx" #运行的组

角色编写

这里角色都统一放在了 /etc/ansible/roles

安装编译时所需要用到的依赖包

[root@ansible ~]# cd /etc/ansible/roles/
[root@ansible roles]# cat init_pkg.yml
#安装源码编译 php、nginx 时所需要用到的依赖包
---
- hosts: all
remote_user: root
tasks:
- name: Install Package
yum: name={{item}} state=installed
with_items:
- gcc-c++
- glibc
- glibc-devel
- glib2
- glib2-devel
- pcre
- pcre-devel
- zlib
- zlib-devel
- openssl
- openssl-devel
- libpng
- libpng-devel
- freetype
- freetype-devel
- libxml2
- libxml2-devel
- bzip2
- bzip2-devel
- ncurses
- curl
- gdbm-devel
- libXpm-devel
- libX11-devel
- gd-devel
- gmp-devel
- readline-devel
- libxslt-devel
- expat-devel
- xmlrpc-c
- libcurl-devel

nginx roles

1)创建相应文件夹

[root@ansible roles]# mkdir -p nginx/{files,handlers,tasks,templates,vars}

2)最终编写效果

[root@ansible roles]# tree nginx
nginx
├── files
│  ├── nginx-1.12.2.tar.gz
│  └── nginx-1.16.0.tar.gz
├── handlers
│  └── main.yml
├── tasks
│  ├── config.yml
│  ├── copypkg.yml
│  ├── group.yml
│  ├── install.yml
│  ├── main.yml
│  ├── service.yml
│  └── user.yml
├── templates
│  ├── nginx.conf.j2
│  ├── nginx_init.j2
│  └── nginx.service.j2
└── vars
└── main.yml
5 directories, 14 files

php roles

1)创建相应文件夹

[root@ansible roles]# mkdir -p php/{files,handlers,tasks,templates,vars}

2)最终编写效果

[root@ansible roles]# tree php
php
├── files
│  └── php-5.6.40.tar.gz
├── handlers
│  └── main.yml
├── tasks
│  ├── config.yml
│  ├── copypkg.yml
│  ├── group.yml
│  ├── install.yml
│  ├── main.yml
│  ├── service.yml
│  └── user.yml
├── templates
│  ├── php-fpm.conf.j2
│  ├── php-fpm.init.j2
│  ├── php-fpm.service.j2
│  └── php.ini.j2
└── vars
└── main.yml
5 directories, 14 files

mysql roles

1)创建相应文件夹

[root@ansible roles]# mkdir -p mysql/{files,handlers,tasks,templates,vars}

2)最终编写效果

[root@ansible roles]# tree mysql
mysql
├── files
├── handlers
│  └── main.yml
├── tasks
│  ├── config.yml
│  ├── install.yml
│  ├── main.yml
│  └── service.yml
├── templates
│  ├── my.cnf6.j2
│  └── my.cnf7.j2
└── vars
5 directories, 7 files

角色执行 playbook 文件编写

[root@ansible roles]# cat nginx_roles.yml
#源码编译安装 nginx
---
- hosts: all
remote_user: root
roles:
- role: nginx
[root@ansible roles]# cat php_roles.yml
#源码编译安装 nginx
---
- hosts: all
remote_user: root
roles:
- role: php
[root@ansible roles]# cat mysql_roles.yml
#yum 安装 MySQL
---
- hosts: all
remote_user: root
roles:
- role: mysql
[root@ansible roles]# cat lnmp.yml
#配置 lnmp, 创建虚拟主机
---
- hosts: all
remote_user: root
roles:
- role: nginx
- role: php
- role: mysql
vars:
PORT: 8081
WEBDIR: "/opt/www"
CONFIGDIR: "/usr/local/nginx/conf/conf.d"
tasks:
- name: create vhost dir
file: name={{WEBDIR}} state=directory owner=www group=www mode=755
- name: create vhost conf
template: src=vhost.conf.j2 dest={{CONFIGDIR}}/vhost.conf
notify: Restart Nginx
- name: create index.php
shell: "echo'<?php phpinfo(); ?>'> {{ WEBDIR}}/index.php"
handlers:
- name: Restart Nginx
service: name=nginx state=restarted
# hostslist 文件准备,这样方便执行,可以在执行 playbook 时指定某台机器上运行
[root@ansible roles]# cat hostlist
192.168.1.31
192.168.1.32
192.168.1.33
192.168.1.36
#所有文件查看
[root@ansible roles]# ll
总用量 28
-rw-r--r--. 1 root root 53 6 4 22:37 hostlist
-rw-r--r--. 1 root root 824 6 5 10:53 init_pkg.yml
-rw-r--r--. 1 root root 646 6 5 12:05 lnmp.yml
drwxr-xr-x. 7 root root 77 6 5 10:44 mysql
-rw-r--r--. 1 root root 81 6 5 10:06 mysql_roles.yml
drwxr-xr-x. 7 root root 77 6 4 15:37 nginx
-rw-r--r--. 1 root root 89 6 4 17:10 nginx_roles.yml
drwxr-xr-x. 7 root root 77 6 4 17:18 php
-rw-r--r--. 1 root root 87 6 4 17:37 php_roles.yml
-rw-r--r--. 1 root root 811 6 5 11:53 vhost.conf.j2

所有文件查看

[root@ansible roles]# tree
.
├── hostlist
├── init_pkg.yml
├── lnmp.yml
├── mysql
│  ├── files
│  ├── handlers
│  │  └── main.yml
│  ├── tasks
│  │  ├── config.yml
│  │  ├── install.yml
│  │  ├── main.yml
│  │  └── service.yml
│  ├── templates
│  │  ├── my.cnf6.j2
│  │  └── my.cnf7.j2
│  └── vars
├── mysql_roles.yml
├── nginx
│  ├── files
│  │  ├── nginx-1.12.2.tar.gz
│  │  └── nginx-1.16.0.tar.gz
│  ├── handlers
│  │  └── main.yml
│  ├── tasks
│  │  ├── config.yml
│  │  ├── copypkg.yml
│  │  ├── group.yml
│  │  ├── install.yml
│  │  ├── main.yml
│  │  ├── service.yml
│  │  └── user.yml
│  ├── templates
│  │  ├── nginx.conf.j2
│  │  ├── nginx_init.j2
│  │  └── nginx.service.j2
│  └── vars
│  └── main.yml
├── nginx_roles.yml
├── php
│  ├── files
│  │  └── php-5.6.40.tar.gz
│  ├── handlers
│  │  └── main.yml
│  ├── tasks
│  │  ├── config.yml
│  │  ├── copypkg.yml
│  │  ├── group.yml
│  │  ├── install.yml
│  │  ├── main.yml
│  │  ├── service.yml
│  │  └── user.yml
│  ├── templates
│  │  ├── php-fpm.conf.j2
│  │  ├── php-fpm.init.j2
│  │  ├── php-fpm.service.j2
│  │  └── php.ini.j2
│  └── vars
│  └── main.yml
├── php_roles.yml
└── vhost.conf.j2
18 directories, 42 files

执行说明

1)单独某一台机器安装 nginx

[root@ansible roles]# ansible-playbook -i hostlist nginx_roles.yml --limit 192.168.1.31

2)单独某一台机器安装 php

[root@ansible roles]# ansible-playbook -i hostlist php_roles.yml --limit 192.168.1.31

3)单独某一台机器安装 mysql

[root@ansible roles]# ansible-playbook -i hostlist mysql_roles.yml --limit 192.168.1.31

4)单独某一台机器部署 lnmp

[root@ansible roles]# ansible-playbook -i hostlist lnmp.yml --limit 192.168.1.31

5)所有机器部署 php

[root@ansible roles]# ansible-playbook php_roles.yml

6)所有机器部署 nginx

[root@ansible roles]# ansible-playbook nginx_roles.yml

7)所有机器部署 mysql

[root@ansible roles]# ansible-playbook mysql_roles.yml

8)所有机器部署 lnmp

[root@ansible roles]# ansible-playbook lnmp.yml

 Ansible 项目实战 LNMP

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5006字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中