用ansible剧本搭建lnmp
首先在主服务器上搭建ansible直接用云yum装就可以,
yum -y install ansible
如果copy报错一下的语句 "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
需要在被分发的服务器上安装支持包 [root@www ~]# mount /dev/sr0 /media/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@www ~]# yum -y install libselinux-python
然后建立roles的标准化模块相应的目录
[root@ansible myroles]# tree /myroles/
/myroles/
├── nginx.yaml #nginx模组入口配置文件
└── roles
└── nginx #nginx原型模组目录
├── files
├── handlers
├── tasks
│ └── main.yaml #nginx模组的tasks任务配置文件
├── templates
└── vars
directories, files
Nginx的剧本书写,在myroles里,跟roles同级
---
- hosts: all
gather_facts: True
roles:
- nginx
然后在Nginx目录下files目录里放置安装包和,安装脚本文件
nginx的搭建脚本
#!/bin/bash
mkdir -p /media/cdrom
umount /dev/sr0 &>/dev/null
mount /dev/sr0 /media/cdrom &>/dev/null
dir=/etc/yum.repos.d
[ -d $dir ] || mkdir -p $dir
cd $dir
mv * /tmp/
cat >/etc/yum.repos.d/local.repo << KOF
[local]
name=localrepo
baseurl=file:///media/cdrom/
KOF
yum -y clean all &>/dev/null
[ $? -eq ] || echo "clean erro"
yum makecache &>/dev/null || echo "erro cache" which "wget"
[ $? -eq ] || /usr/bin/yum -y install wget &>/dev/null
/usr/bin/wget http://mirrors.aliyun.com/repo/epel-6.repo
[ $? -eq ] || (/bin/echo "yun源出错" && exit)
/usr/bin/yum -y clean all &>/dev/null
/usr/bin/yum makecache &>/dev/null
[ $? -eq ] || (/bin/echo "yun缓存错误" && exit)
/usr/bin/yum -y install pcre-deved openssl-devel &>/dev/null
[ $? -eq ] || /bin/echo "pcre error"
useradd -M -s /sbin/nologin nginx &>dev/null
cd ~
tar xf nginx-1.10..tar.gz -C /usr/src/
cd /usr/src/nginx-1.10./
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.10./ --with-http_stub_status_module --with-http_ssl_module && make && make install &>/dev/null
在tasks里创建个main.yaml主要任务剧本
- name: t1
copy: src=nginx-1.10..tar.gz dest=/root/
register: ttt
- debug: var=ttt
- name: t2
script: nginx.sh
register: rrr
- debug: var=rrr
回到mysoles里执行nginx.yaml剧本
[root@bogon myroles]# ansible-playbook nginx.yaml PLAY [all] ************************************************************************************************* TASK [Gathering Facts] *************************************************************************************
ok: [web1]
ok: [web2] TASK [nginx : t1] ******************************************************************************************
ok: [web2]
ok: [web1] TASK [nginx : debug] ***************************************************************************************
ok: [web1] => {
"ttt": {
"changed": false,
"checksum": "1bafb1557b8d5f992714c0dcbde77036bde98547",
"dest": "/root/nginx-1.10.2.tar.gz",
"diff": {
"after": {
"path": "/root/nginx-1.10.2.tar.gz"
},
"before": {
"path": "/root/nginx-1.10.2.tar.gz"
}
},
"failed": false,
"gid": ,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/nginx-1.10.2.tar.gz",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": ,
"state": "file",
"uid":
}
}
ok: [web2] => {
"ttt": {
"changed": false,
"checksum": "1bafb1557b8d5f992714c0dcbde77036bde98547",
"dest": "/root/nginx-1.10.2.tar.gz",
"diff": {
"after": {
"path": "/root/nginx-1.10.2.tar.gz"
},
"before": {
"path": "/root/nginx-1.10.2.tar.gz"
}
},
"failed": false,
"gid": ,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/nginx-1.10.2.tar.gz",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": ,
"state": "file",
"uid":
}
} TASK [nginx : t2] ******************************************************************************************
changed: [web2]
changed: [web1] TASK [nginx : debug] ***************************************************************************************
ok: [web1] => {
"rrr": {
"changed": true,
"failed": false,
"rc": ,
"stderr": "Shared connection to 192.168.200.131 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.131 closed."
],
"stdout": "/usr/bin/wget\r\n--2018-11-12 19:02:03-- http://mirrors.aliyun.com/repo/epel-6.repo\r\nResolving mirrors.aliyun.com... failed: Name or service not known.\r\nwget: unable to resolve host address “mirrors.aliyun.com”\r\nyun源出错\r\n",
"stdout_lines": [
"/usr/bin/wget",
"--2018-11-12 19:02:03-- http://mirrors.aliyun.com/repo/epel-6.repo",
"Resolving mirrors.aliyun.com... failed: Name or service not known.",
"wget: unable to resolve host address “mirrors.aliyun.com”",
"yun源出错"
]
}
}
ok: [web2] => {
"rrr": {
"changed": true,
"failed": false,
"rc": ,
"stderr": "Shared connection to 192.168.200.133 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.133 closed."
],
"stdout": "/usr/bin/wget\r\n--2018-11-12 16:03:20-- http://mirrors.aliyun.com/repo/epel-6.repo\r\nResolving mirrors.aliyun.com... 122.72.3.220, 122.72.3.219, 122.72.3.221, ...\r\nConnecting to mirrors.aliyun.com|122.72.3.220|:80... connected.\r\nHTTP request sent, awaiting response... 200 OK\r\nLength: 664 [application/octet-stream]\r\nSaving to: “epel-6.repo”\r\n\r\n\r 0% [ ] 0 --.-K/s \r100%[======================================>] 664 --.-K/s in 0s \r\n\r\n2018-11-12 16:03:21 (62.4 MB/s) - “epel-6.repo” saved [664/664]\r\n\r\n",
"stdout_lines": [
"/usr/bin/wget",
"--2018-11-12 16:03:20-- http://mirrors.aliyun.com/repo/epel-6.repo",
"Resolving mirrors.aliyun.com... 122.72.3.220, 122.72.3.219, 122.72.3.221, ...",
"Connecting to mirrors.aliyun.com|122.72.3.220|:80... connected.",
"HTTP request sent, awaiting response... 200 OK",
"Length: 664 [application/octet-stream]",
"Saving to: “epel-6.repo”",
"",
"",
" 0% [ ] 0 --.-K/s ",
"100%[======================================>] 664 --.-K/s in 0s ",
"",
"2018-11-12 16:03:21 (62.4 MB/s) - “epel-6.repo” saved [664/664]",
""
]
}
} PLAY RECAP *************************************************************************************************
web1 : ok= changed= unreachable= failed=
web2 : ok= changed= unreachable= failed=
这样Nginx服务就先简单的搭建成功,需要主要目前没有任何的配置文件和启动服务
接下来我搭建mysql
roles的模板,
[root@bogon ~]# tree /myroles/
/myroles/
├── mysql.retry
├── mysql.yaml
├── nginx.retry
├── nginx.yaml
└── roles
├── mysql
│?? ├── files
│?? │?? ├── mysql-5.5.-linux2.-x86_64.tar.gz
│?? │?? ├── mysql.sh
│?? │?? └── yum.sh
│?? ├── handlers
│?? ├── tasks
│?? │?? └── main.yaml
│?? ├── templates
│?? └── vars
├── nginx
│?? ├── files
│?? │?? ├── nginx-1.10..tar.gz
│?? │?? └── nginx.sh
│?? ├── handlers
│?? ├── tasks
│?? │?? └── main.yaml
│?? ├── templates
│?? └── vars
└── php
├── files
├── handlers
├── tasks
├── templates
└── vars
mysql.yaml的内容
---
- hosts: all
gather_facts: True
roles:
- mysql
tasks里边的任务
[root@bogon tasks]# vim main.yaml - name: t1
copy: src=mysql-5.5.-linux2.-x86_64.tar.gz dest=/root/
register: ttt
- debug: var=ttt
#- name: yum
# script: yum.sh
# register: zzz
#- debug: var=zzz
- name: t2
script: mysql.sh
register: rrr
- debug: var=rrr
~
files里边的脚本
yum的脚本
#!/bin/bash
mkdir -p /media/cdrom
umount /dev/sr0 &>/dev/null
mount /dev/sr0 /media/cdrom &>/dev/null
dir=/etc/yum.repos.d
[ -d $dir ] || mkdir -p $dir
cd $dir
mv * /tmp/
cat >/etc/yum.repos.d/local.repo << KOF
[local]
name=localrepo
baseurl=file:///media/cdrom/
KOF
yum -y clean all &>/dev/null
[ $? -eq ] || echo "clean erro"
yum makecache &>/dev/null || echo "erro cache"
which "wget"
[ $? -eq ] || /usr/bin/yum -y install wget &>/dev/null
/usr/bin/wget http://mirrors.aliyun.com/repo/epel-6.repo
[ $? -eq ] || (/bin/echo "yun源出错" && exit)
/usr/bin/yum -y clean all &>/dev/null
/usr/bin/yum makecache &>/dev/null
[ $? -eq ] || (/bin/echo "yun缓存错误" && exit)
/usr/bin/yum -y install pcre-deved openssl-devel &>/dev/null
[ $? -eq ] || /bin/echo "pcre error"
mysl的安装脚本(需要注意的是这里使用的是二进制压缩包,简单的脚本,不严谨,可以执行成功)
#!/bin/bash
groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
cd ~
tar xf mysql-5.5.-linux2.-x86_64.tar.gz -C /usr/local/
cd /usr/local/
mv mysql-5.5.-linux2.-x86_64 mysql-5.5.
ln -s mysql-5.5. mysql
[ -d /usr/local/mysql/data ] || mkdir -p /usr/local/mysql/data
chown -R mysql.mysql /usr/local/mysql
yum -y install libaio
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
执行mysql剧本
[root@bogon myroles]# ansible-playbook mysql.yaml
[root@bogon myroles]# ansible-playbook mysql.yaml PLAY [all] ***************************************************************************************************************************************************************** TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [web1] TASK [mysql : t1] **********************************************************************************************************************************************************
ok: [web1] TASK [mysql : debug] *******************************************************************************************************************************************************
ok: [web1] => {
"ttt": {
"changed": false,
"checksum": "1861329e637aca5e143c436fd795d28ed8f10729",
"dest": "/root/mysql-5.5.32-linux2.6-x86_64.tar.gz",
"diff": {
"after": {
"path": "/root/mysql-5.5.32-linux2.6-x86_64.tar.gz"
},
"before": {
"path": "/root/mysql-5.5.32-linux2.6-x86_64.tar.gz"
}
},
"failed": false,
"gid": ,
"group": "root",
"mode": "",
"owner": "root",
"path": "/root/mysql-5.5.32-linux2.6-x86_64.tar.gz",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": ,
"state": "file",
"uid":
}
} TASK [mysql : t2] **********************************************************************************************************************************************************
changed: [web1] TASK [mysql : debug] *******************************************************************************************************************************************************
ok: [web1] => {
"rrr": {
"changed": true,
"failed": false,
"rc": ,
"stderr": "Shared connection to 192.168.200.131 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.131 closed."
],
"stdout": "groupadd: group 'mysql' already exists\r\nuseradd: user 'mysql' already exists\r\nmv: cannot move `mysql-5.5.32-linux2.6-x86_64' to `mysql-5.5.32/mysql-5.5.32-linux2.6-x86_64': Directory not empty\r\nln: creating symbolic link `mysql/mysql-5.5.32': File exists\r\nLoaded plugins: fastestmirror\r\nLoading mirror speeds from cached hostfile\r\nSetting up Install Process\r\nPackage libaio-0.3.107-10.el6.x86_64 already installed and latest version\r\nNothing to do\r\nInstalling MySQL system tables...\r\nOK\r\nFilling help tables...\r\nOK\r\n\r\nTo start mysqld at boot time you have to copy\r\nsupport-files/mysql.server to the right place for your system\r\n\r\nPLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !\r\nTo do so, start the server, then issue the following commands:\r\n\r\n/usr/local/mysql/bin/mysqladmin -u root password 'new-password'\r\n/usr/local/mysql/bin/mysqladmin -u root -h www.sunan.com password 'new-password'\r\n\r\nAlternatively you can run:\r\n/usr/local/mysql/bin/mysql_secure_installation\r\n\r\nwhich will also give you the option of removing the test\r\ndatabases and anonymous user created by default. This is\r\nstrongly recommended for production servers.\r\n\r\nSee the manual for more instructions.\r\n\r\nYou can start the MySQL daemon with:\r\ncd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &\r\n\r\nYou can test the MySQL daemon with mysql-test-run.pl\r\ncd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl\r\n\r\nPlease report any problems with the /usr/local/mysql/scripts/mysqlbug script!\r\n\r\nStarting MySQL SUCCESS! \r\n",
"stdout_lines": [
"groupadd: group 'mysql' already exists",
"useradd: user 'mysql' already exists",
"mv: cannot move `mysql-5.5.32-linux2.6-x86_64' to `mysql-5.5.32/mysql-5.5.32-linux2.6-x86_64': Directory not empty",
"ln: creating symbolic link `mysql/mysql-5.5.32': File exists",
"Loaded plugins: fastestmirror",
"Loading mirror speeds from cached hostfile",
"Setting up Install Process",
"Package libaio-0.3.107-10.el6.x86_64 already installed and latest version",
"Nothing to do",
"Installing MySQL system tables...",
"OK",
"Filling help tables...",
"OK",
"",
"To start mysqld at boot time you have to copy",
"support-files/mysql.server to the right place for your system",
"",
"PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !",
"To do so, start the server, then issue the following commands:",
"",
"/usr/local/mysql/bin/mysqladmin -u root password 'new-password'",
"/usr/local/mysql/bin/mysqladmin -u root -h www.sunan.com password 'new-password'",
"",
"Alternatively you can run:",
"/usr/local/mysql/bin/mysql_secure_installation",
"",
"which will also give you the option of removing the test",
"databases and anonymous user created by default. This is",
"strongly recommended for production servers.",
"",
"See the manual for more instructions.",
"",
"You can start the MySQL daemon with:",
"cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &",
"",
"You can test the MySQL daemon with mysql-test-run.pl",
"cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl",
"",
"Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!",
"",
"Starting MySQL SUCCESS! "
]
}
} PLAY RECAP *****************************************************************************************************************************************************************
web1 : ok= changed= unreachable= failed=
如果报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
是说明找不到sockt启动文件,第一个办法是修改配置文件的路径,
[root@www ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links= [mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
第二个办法是直接做个软连接就可以
ln -s /var/lib/mysql/* /tmp/
我们的mysql就安装成功了,接下来我们安装php
roles的树形结构
[root@bogon php]# tree /myroles/
/myroles/
├── mysql.retry
├── mysql.yaml
├── nginx.retry
├── nginx.yaml
├── php.retry
├── php.yaml
└── roles
├── mysql
│ ├── files
│ │ ├── mysql-5.5.-linux2.-x86_64.tar.gz
│ │ ├── mysql.sh
│ │ └── yum.sh
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── nginx
│ ├── files
│ │ ├── nginx-1.10..tar.gz
│ │ └── nginx.sh
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
└── php
├── files
│ ├── libiconv-1.14.tar.gz
│ ├── php-5.3..tar.gz
│ └── php.sh
├── handlers
├── tasks
│ └── main.yaml
├── templates
└── vars
php的剧本与roles平级的
[root@bogon myroles]# vim php.yaml
---
- hosts: all
gather_facts: True
roles:
- php
php的tasks的剧本
[root@bogon tasks]# vim main.yaml - name: t1
copy: src=php-5.3..tar.gz dest=/root/
register: ttt
- debug: var=ttt
- name: t3
copy: src=libiconv-1.14.tar.gz dest=/root/
register: ttt
- debug: var=ttt
- name: t2
script: php.sh
register: rrr
- debug: var=rrr
安装php的脚本
[root@bogon files]# pwd
/myroles/roles/php/files
[root@bogon files]# vim php.sh
#!/bin/bash
yum -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
cd ~
tar xf libiconv-1.14.tar.gz -C /usr/src
cd /usr/src/libiconv-1.14
./configure --prefix=/usr/local/libiconv && make && make install
yum -y install libmcrypt-devel mhash mcrypt
useradd -s /sbin/nologin -M www
cd ~
tar xf php-5.3..tar.gz -C /usr/src/
cd /usr/src/php-5.3. && ./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp
make && make install
ln -s /usr/local/php5.3.28/ /usr/local/php
cd /usr/src/php-5.3.
cp php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
/usr/local/php/sbin/php-fpm
用ansible剧本搭建lnmp的更多相关文章
- ansible+playbook 搭建lnmp环境
用三台机器 做ansible+playbook 搭建lnmp环境 IP分配 ansible 主机192.168.202.132 lnmp第一台主机 192.168.202.131 lnmp第一台主机 ...
- CentOS6.6搭建LNMP环境
CentOS6.6搭建LNMP环境 1.设置yum源,本地安装依赖包 1 yum -y install gcc gcc-c++ automake autoconf libtool make 2.下载依 ...
- 源码搭建LNMP
源码安装LNMP 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...
- CentOS下Web服务器环境搭建LNMP一键安装包
CentOS下Web服务器环境搭建LNMP一键安装包 时间:2014-09-04 00:50来源:osyunwei.com 作者:osyunwei.com 举报 点击:3797次 最新版本:lnmp- ...
- CentOS 搭建LNMP服务器和LAMP服务器
CentOS 搭建LNMP服务器 方法一:yum安装 1.更新YUM源 wget http://www.atomicorp.com/installers/atomic #下载atomic自动更新Y ...
- CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)【转】
转自:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.5 64位 目标:搭建LNMP(Linux + ...
- 源码搭建lnmp平台
lnmp平台是指利用linux操作系统,nginx服务器,mysql数据库和php语言搭建高性能web服务器,负载均衡器和邮件代理服务器. 原理图:‘
- 图文详解如何快捷搭建LNMP服务环境
上一篇与大家一起学习了下如何搭建LAMP环境的知识,今天小编再和大家分享下如何快捷地搭建LNMP环境,并搭建起一个网站.Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/S ...
- Docker进阶之八:搭建LNMP网站平台实战
搭建LNMP网站平台实战 LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写.L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可 ...
随机推荐
- 爬虫之selenium使用
详细使用链接: 点击链接 selenium介绍: selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质 ...
- python之sqlalchemy使用
一.介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据API执行SQL并 ...
- python 面向对象编程学习总结
面向对象是个抽象的东西,概念比较多,下面会一一介绍. 一.类和实例 类(Class)和实例(Instance)是面向对象最重要的概念. 类是指抽象出的模板.实例则是根据类创建出来的具体的“对象”,每个 ...
- python基础之if语句
python之if语句 通用格式 if <test1>: <do something> elif: <do something> else: <do some ...
- cocos代码研究(12)UI之Widget学习笔记
理论基础 Widget类,所有UI控件的基类. 这类继承自ProtectedNode和LayoutParameterProtocol. 如果你想实现自己的UI控件,你应该继承这个类. 被 VideoP ...
- json反序列化快捷实体类
有时候我们反序列化一个json串只为了提取里面的数据,而且json串的层级结构可能会比较复杂,定义对应的实体类会多而杂,有时还不一定能达到想要的效果. 则可以关注下以下两个类: java : ...
- loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...
- Sybase IQ使用过程中注意事项
Sybase IQ使用过程中注意事项 1,字母大小写比对不敏感,也就是在值比对判断时大小写字母都一样; 2,等值,或<>判断,系统默认对等式两边比对值去右边空格再进行比较: 3,GROUP ...
- 20145211 《网络对抗》Exp8 Web基础
20145211 <网络对抗>Exp8 Web基础 本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法 ...
- linux下如何使用split
答: 切割文件hello,以每个文件最大10MiB来切割,切割好的文件名前缀为hello.,后缀为二位的数字,切割之后的名字为hello.01,hello.02等等 split -b 10M - ...