首先在主服务器上搭建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的更多相关文章

  1. ansible+playbook 搭建lnmp环境

    用三台机器 做ansible+playbook 搭建lnmp环境 IP分配 ansible 主机192.168.202.132 lnmp第一台主机 192.168.202.131 lnmp第一台主机 ...

  2. CentOS6.6搭建LNMP环境

    CentOS6.6搭建LNMP环境 1.设置yum源,本地安装依赖包 1 yum -y install gcc gcc-c++ automake autoconf libtool make 2.下载依 ...

  3. 源码搭建LNMP

      源码安装LNMP 作者:尹正杰   版权声明:原创作品,谢绝转载!否则将追究法律责任.       欢迎加入:高级运维工程师之路 598432640 前言:非常简单的一个平台LNMP,在生产实际环 ...

  4. CentOS下Web服务器环境搭建LNMP一键安装包

    CentOS下Web服务器环境搭建LNMP一键安装包 时间:2014-09-04 00:50来源:osyunwei.com 作者:osyunwei.com 举报 点击:3797次 最新版本:lnmp- ...

  5. CentOS 搭建LNMP服务器和LAMP服务器

    CentOS 搭建LNMP服务器 方法一:yum安装 1.更新YUM源 wget http://www.atomicorp.com/installers/atomic   #下载atomic自动更新Y ...

  6. CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)【转】

    转自:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.5 64位 目标:搭建LNMP(Linux + ...

  7. 源码搭建lnmp平台

    lnmp平台是指利用linux操作系统,nginx服务器,mysql数据库和php语言搭建高性能web服务器,负载均衡器和邮件代理服务器. 原理图:‘

  8. 图文详解如何快捷搭建LNMP服务环境

    上一篇与大家一起学习了下如何搭建LAMP环境的知识,今天小编再和大家分享下如何快捷地搭建LNMP环境,并搭建起一个网站.Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/S ...

  9. Docker进阶之八:搭建LNMP网站平台实战

    搭建LNMP网站平台实战 LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写.L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可 ...

随机推荐

  1. Django - 常用配置

    一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...

  2. java-mybaits-010-mybatis-spring-使用 SqlSession、注入映射器

    一. SqlSession概述 在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession.一旦你获得一个 session 之后,你可以使用它来执行映射语句, ...

  3. spring 返回xml数据

    要将对象以xml数据形式返回:参考:https://blog.csdn.net/Pnoter/article/details/52218363 1:创建对象时应该填上xml注解,这样才能将对象转换为x ...

  4. MapReduce: map读取文件的过程

    我们的输入文件 hello0, 内容如下: xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown 逻辑上有3条记录, 它们以 ...

  5. ArrayBuffer:类型化数组

    类型化数组是JavaScript操作二进制数据的一个接口. 这要从WebGL项目的诞生说起,所谓WebGL,就是指浏览器与显卡之间的通信接口,为了满足JavaScript与显卡之间大量的.实时的数据交 ...

  6. 2017 java期末上机练习

    仅供参考! 一.最大值.最小值.平均数 package examination; import java.util.Arrays; import java.util.Scanner; /** * 1. ...

  7. 20172305 2018-2019-1 《Java软件结构与数据结构》第二周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第二周学习总结 教材学习内容总结 本周内容主要为书第三章和第四章的内容: 第三章(以数组来替代栈的作用) 集合(聚集 ...

  8. Android Studio安装与使用

    2013年谷歌推出android studio后,单独支持android开发,这是基于Java语言集成开发环境IntelliJ搭建的IDE.特别在android studio1.0稳定版出来后,谷歌将 ...

  9. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  10. 第二章 第二个spring-boot程序

    上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用. 1. ...