ansible-playbook编译安装nginx
ansible-playbook编译安装nginx
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
首先本机ansible安装号LANM,文件和模板都是从本机复制到目标LANM
本机ansible:IP:192.168.0.250
目标LANM:IP:192.168.0.201
1、交互式expect 免密钥
pwd
/root/ansible/roles/sh[root@bogon sh]# cat ip.txt #要同步免秘钥的ip地址
192.168.0.201
[root@bogon sh]# cat auto_ssh.sh
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh-copy-id -i /root/.ssh/id_dsa.pub $username@$hostname
expect {#first connect, no public key in ~/.ssh/known_hosts"Are you sure you want to continue connecting (yes/no)?" {send "yes\r"expect "password:"send "$password\r"}#already has public key in ~/.ssh/known_hosts"password:" {send "$password\r"}"Now try logging into the machine" {#it has authorized, do nothing!}}
expect eof
```bash
#!/bin/bash
/usr/bin/yum -y install expect > /dev/null 2>&1
rm -rf /root/.ssh/id_dsa
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
user="root"
password="123456"
ip="cat /root/ansible/roles/sh/ip.txt"
for i in `$ip`
do/root/ansible/roles/sh/auto_ssh.sh $user $password $i
done
chmod 777 auto_ssh.sh
chmod 777 ssh_key.sh
sh ssh_key.sh
2、主机清单
cat /etc/ansible/hosts
[nginx]
192.168.0.201 #主机清单
3、目录结构
4、剧本集合
[root@bogon tasks]# pwd #路径
/root/ansible/roles/nginx/tasks
[root@bogon tasks]# ls #剧本和目录
configure.yml copynginx.yml copyselinux.yml files file.yml firewallstop.yml main.yml restart.yml shell.yml tarnginx.yml template.yml user.yml yum.yml
[root@bogon tasks]# cat configure.yml nginx
- name: configure make make install #剧本编译安装shell: cd /usr/src/nginx-1.6.0/;./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@bogon tasks]# cat copynginx.yml
- name: copy nginx.tar #复制nginx软件包copy: src=nginx-1.6.0.tar.gz dest=/usr/src/nginx-1.6.0.tar.gz
- name: copy nginxstart.sh #复制nginx启动脚本,copy: src=nginxstart.sh dest=/etc/init.d/nginx mode=777
[root@bogon tasks]# cat copyselinux.yml #关闭selinux
- name: copy selinux #复制本机selinux到目标 文件,关闭selinuxcopy: src=/etc/selinux/config dest=/etc/selinux/config
[root@bogon tasks]# cat file.yml
- name: create nginx file=link(ln) #创建软连接file: src=/usr/local/nginx/sbin/nginx dest=/usr/local/sbin/nginx state=link
- name: create directory baidu #创建目录,并777权限file: path=/var/www/baidu/ state=directory mode=777
- name: create touch baidu.access.log #创建文件,并777权限file: path=/var/www/baidu/baidu.access.log state=touch mode=777
[root@bogon tasks]# cat firewallstop.yml
- name: firewalld stop #关闭防火墙,禁用开机自动启动service: name=firewalld state=stopped enabled=no
cat restart.yml
- name: restart nginx #重启nginxshell: /usr/bin/killall -s QUIT nginx; /etc/init.d/nginx start
- name: chmod rc.local 777 #开机自动启动文件授权权限file: dest=/etc/rc.d/rc.local mode=777
- name: enabled nginx #加入开机自动启动shell: echo "/etc/init.d/nginx start" >> /etc/rc.d/rc.local
[root@bogon tasks]# cat shell.yml #创建index.html
- name: create index.html #创建 index.htmlshell: echo wo shi baidu server > /var/www/baidu/index.html
[root@bogon tasks]# cat tarnginx.yml
- name: tar nginx #解压nginx软件包shell: cd /usr/src/; tar zxf nginx-1.6.0.tar.gz -C /usr/src/
[root@bogon tasks]# cat template.yml
- name: copy nginx.conf #复制nginx配置文件j2模板template: src=nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf
[root@bogon tasks]# cat user.yml
- name: create nginx #创建nginx用户user: name=nginx system=yes state=present
[root@bogon tasks]# cat yum.yml
- name: install pcre-devel #安装需要软件包yum: name=pcre-devel state=present
- name: install zlib-develyum: name=zlib-devel state=present
- name: install gcc-c++yum: name=gcc-c++ state=present
- name: install elinksyum: name=elinks state=present
- name: install psmiscyum: name=psmisc state=present
- name: remove httpdyum: name=httpd state=absent
5、主文件调用剧本
[root@bogon tasks]# cat main.yml ##主文件调用剧本
- include: copyselinux.yml
- include: firewallstop.yml
- include: yum.yml
- include: user.yml
- include: copynginx.yml
- include: tarnginx.yml
- include: configure.yml
- include: file.yml
- include: template.yml
- include: shell.yml
- include: restart.yml
5、nignx启动脚本和nginx软件包存放位置
[root@bogon files]#pwd #路径
/root/ansible/roles/nginx/tasks/files
[root@bogon files]# ls
nginx-1.6.0.tar.gz nginxstart.sh
[root@bogon files]# cat nginxstart.sh #nginx脚本
#! /bin/sh
#chkconfig: - 33 33
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in start)$PROG;;stop)kill -s QUIT $(cat $PIDF) &> /dev/null;;restart)$0 stop &> /dev/null$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1 esacexit 0
5、模板nginx.conf.j2
[root@bogon templates]# cat nginx.conf.j2 #模板nginx.conf.j2#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name {{ ansible_ens33.ipv4.address }};charset utf-8;access_log logs/baidu.access.log;location /{root /var/www/baidu;index index.html index.php;}location ~ \.php$ { # 添加root /var/www/baidu; #添加fastcgi_pass 127.0.0.1:9000; # 添加fastcgi_index index.php; # 添加include fastcgi.conf; # 添加}}server {listen 80;server_name localhost;location /status { stub_status on; access_log off; }#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
6、角色剧本
[root@bogon ansible]# pwd
/root/ansible
[root@bogon ansible]# ls
nginx_role.yml roles
[root@bogon ansible]# cat nginx_role.yml #角色剧本
---
- hosts: nginxremote_user: rootroles:- role: nginx
[root@bogon ansible]#ansible-playbook nginx_role.yml #执行剧本
7浏览器验证效果
#验证效果
关闭selinux,
关闭防火墙
检查yum安装
账号检查
检查nginx.tar 和nginx启动脚本复制情况和解压nginx
检查编译安装
检查软连接
检查创建baidu目录
检查创建baidu_access.log日志文件
检查nnginx.conf.j2 =====nginx.conf模板文件复制情况
检查index.html
检查nginx进程和端口
ansible-playbook编译安装nginx相关推荐
- ansible-playbook 手工编译安装nginx
虽然nginx也可以通过yum安装,但是如何使用源码包安装并自定义开启一些nginx功能模块,并且通过ansible下发到被管理集群呢?下面给给位看官提供一个具体实例以供参考. 首先我们需要手工编译好 ...
- Ubuntu 17.04 编译安装 Nginx 1.9.9 配置 https 免费证书
Ubuntu 17.04 编译安装 Nginx 1.9.9 配置 https 免费证书 安装 Nginx 安装依赖 $ apt-get update $ apt-get install build-e ...
- Ubuntu 16.04源码编译安装nginx 1.10.0
一.下载相关的依赖库 pcre 下载地址 http://120.52.73.43/jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.t ...
- CentOS上编译安装Nginx
背景 为了解决CentOS7.2自带的Nginx上没有auth-request支持的问题,需要自己手工编译安装Nginx . 安装依赖 更新yum yum update -y yum -y insta ...
- Centos7 编译安装 Nginx、MariaDB、PHP
前言 本文主要大致介绍CentOS 7下编译安装Nginx.MariaDB.PHP.面向有Linux基础且爱好钻研的朋友.技艺不精,疏漏再所难免,还望指正. 环境简介: 系统: CentOS 7,最小 ...
- CentOS 6下编译安装Nginx
CentOS 6下编译安装Nginx By:老宁 一.准备make环境 yum -y install gcc gcc-c++ automake autoconf libtool make 二.准备 ...
- ubuntu14.04 nginx php编译安装,Ubuntu 14.04 编译安装 Nginx
在Ubuntu 14.04下编译安装 Nginx过程笔记. 下载源码包 nginx 地址: http://nginx.org/en/download.html 编译前先安装两个包: 直接编译安装会碰到 ...
- CentOS 6.5编译安装Nginx+MySQL+PHP
一.配置防火墙,开启80端口.3306端口,关闭SELINUX [root@Zabbix ~]# vim /etc/sysconfig/iptables -A INPUT -i lo -j ACCEP ...
- 【资料整理】编译安装nginx
[nginx]编译安装nginx 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
最新文章
- 工业界常用的三维重建技术有哪些?
- 如何快速评估16S rRNA基因引物的覆盖率及特异性
- Silverlight获取WebHost配置信息--WebClient和XmlSerializer模拟
- [转]java垃圾回收之循环引用
- [vue] 说说你对Object.defineProperty的理解
- CSS 中 的 margin、border、padding 区别 (内边距、外边距)
- 判断字符串是否为回文(信息学奥赛一本通-T1146)
- windows下的安装与使用curl实现命令行访问Web网站
- 【PM2.5预测】基于matlab灰色模型PM2.5预测【含Matlab源码 499期】
- 天天生鲜页面设计——网站首页
- 使用Python进行局域网传输文件两种方法
- 三分钟带你快速看懂电子数据取证! | 打击网络犯罪必备知识
- 高分三号卫星GF-3极化SAR
- 免费公网动态IP方案
- 白杨SEO:你知道SEO这个工作被误解或夸大有哪些吗?SEO不是万能的
- JS实现俄罗斯方块小游戏
- codeforces 645 D Robot Rapping Results Report 【树形dp】
- 学习布局(21)HTML5新标签
- MySql函数 - DATE_ADD()函数
- Binder Java层实现(一):IBinder/IInterface/Binder/Stub