Ansible项目实战lnmp

项目规划

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

说明: 将nginx和php源码包放到对应的角色文件下的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..tar.gz
│   └── nginx-1.16..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 directories, 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..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 directories, 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 directories, 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:
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= - 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
总用量
-rw-r--r--. root root 6月 : hostlist
-rw-r--r--. root root 6月 : init_pkg.yml
-rw-r--r--. root root 6月 : lnmp.yml
drwxr-xr-x. root root 6月 : mysql
-rw-r--r--. root root 6月 : mysql_roles.yml
drwxr-xr-x. root root 6月 : nginx
-rw-r--r--. root root 6月 : nginx_roles.yml
drwxr-xr-x. root root 6月 : php
-rw-r--r--. root root 6月 : php_roles.yml
-rw-r--r--. root root 6月 : 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..tar.gz
│   │   └── nginx-1.16..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..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 directories, 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

如需源码及软件包联系我

最新文章

  1. 跟我学Windows Azure 三 使用vs2013创建windows azure web site
  2. 【Beta】第一次任务发布
  3. eclipse自动部署问题
  4. 《BI项目笔记》基于雪花模型的维度设计
  5. jquery mobile 教程
  6. python---字符编码
  7. linux中软链接打包、计算以及同步
  8. UF访问,一些对用友最新的旗舰级产品U9一些引进(图像)
  9. Android 文字过长TextView如何自动截断并显示成省略号
  10. 高德地图 地铁图adcode 城市代码
  11. 插件使用一进度条---nprogress
  12. 关于K8S证书生成方面的脚本草稿
  13. Python3 tkinter基础 Button command 单击按钮 在console中打印文本
  14. NHibernate.3.0.Cookbook第一章第六节Handling versioning and concurrency的翻译
  15. Python - 3MySQL 数据库连接
  16. 3_bootsrap布局容器
  17. 【WPF】生成二维码
  18. 【GCD】AtCoder Grand Contest 018 A - Getting Difference
  19. L138
  20. 【网络】默认路由、RIPv2、OSPF、EIGRP配置(全网全通)

热门文章

  1. springmvc,controller层在接收浏览器url传来的参数带中文乱码问题。
  2. IntelliJ IDEA 2019.2已经可以利用补丁永久破解激活了(持续更新)
  3. 记C# 调用虹软人脸识别 那些坑
  4. 【06月19日】A股滚动市盈率PE最低排名
  5. 4 实战CPU上下文
  6. idea从github导入maven项目
  7. Python 3.X 练习集100题 01
  8. Docker下安装kafka
  9. 【转帖】H5 手机 App 开发入门:概念篇
  10. [CF852E]Casinos and travel(2019-11-15考试)