怎么申请一个网站,外协加工订单网,长沙sem培训,防窜货管理系统开发通过学习ansible自动化运维#xff0c;初步对ansible有了一定的了解#xff0c;此次分享两个案例#xff0c;希望对大家有所帮助 案例一#xff1a;自动化安装nginx
本次案例目的是ansible自动化安装nginx并配置
首先创建如图所示目录 在主机上安装好nginx#xff0c;如…通过学习ansible自动化运维初步对ansible有了一定的了解此次分享两个案例希望对大家有所帮助 案例一自动化安装nginx
本次案例目的是ansible自动化安装nginx并配置
首先创建如图所示目录 在主机上安装好nginx如下
使用wget下载nginx包下载地址http://mirrors.sohu.com/nginx/nginx-1.9.6.tar.gz
对nginx进行解压
tar -zxvf nginx-1.9.6.tar.gz -C /usr/local
cd /usr/local/nginx-1.9.6
./configure --prefix/usr/local/nginx
若是报错根据提示安装好缺少的依赖
make makeinstall
修改/etc/init.d/nginx 内容如下
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN/usr/local/nginx/sbin/nginx
NGINX_CONF/usr/local/nginx/conf/nginx.conf
NGINX_PID/usx/local/nginx/logs/nginx.pid
RETVAL0
progNginxstart()
{echo -n $Starting $prog: mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL$?echoreturn $RETVAL
}
stop()
{echo -n $Stopping $prog: killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL$?echoreturn $RETVAL
}
reload()
{echo -n $Reloading $prog: killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL$?echoreturn $RETVAL
}
restart()
{stopstart
}
configtest()
{$NGINX_SBIN -c $NGINX_CONF -treturn 0
}
case $1 instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $Usage: $0 {start|stop|reload|restart|configtest}RETVAL1
esac
exit $RETVAL
授予权限
chmod 777 /etc/init.d/nginx
清空该文件并进行如下配置/usr/local/nginx/conf/nginx.conf
user nobody nobody; #定义nginx运行的用户和用户组
worker_processes 2; #nginx进程数一般为CPU总核心数
error_log /usr/local/nginx/logs/nginx_error.log crit; #全局错误日志定义类型
pid /usr/local/nginx/logs/nginx.pid; #进程文件
worker_rlimit_nofile 51200;
events #工作模式与连接数上限
{
use epoll;
worker_connections 6000;
}
http #http下的一些配置
{
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip $remote_addr $http_x_forwarded_for [$time_local]
$host $request_uri $status
$http_referer $http_user_agent;
sendfile on; #开启高效文件传输模式
tcp_nopush on; #防止网络阻塞
keepalive_timeout 30; #长连接超时时间单位为秒
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on; #防止网络阻塞
gzip on; #开启gzip压缩输出
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server #虚拟主机配置
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name;
}
}
}
启动服务
service nginx start 将nginx的压缩包移动至目标目录下复制启动脚本和配置文件到目标目录下
mv nginx-1.9.6.tar.gz /etc/ansible/nginx_install/roles/install/files/
cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
在nginx_install目录下编写启动文件install.yml
---
- hosts: testremote_user: rootgather_facts: Trueroles:- common- install 在roles/common/tasks/main.yml文件下编写需要安装的依赖文件
- name: install initialization require softwareyum: name{{ item }} stateinstalledwith_items:- zlib-devel- pcre-devel- gcc 在 roles/install/vars/main.yml文件下定义各种变量
nginx_user: www
nginx_port: 80
nginx_basedir: /usr/local/nginx 在roles/install/tasks/copy.yml文件下编写需要复制的文件
- name: Copy Nginx Software #复制nginx安装包copy: srcnginx-1.9.6.tar.gz dest/tmp/nginx-1.9.6.tar.gz ownerroot grouproot
- name: Uncompression Nginx Software #解压nginxshell: tar zxf /tmp/nginx-1.9.6.tar.gz -C /usr/local/
- name: Configure Nginx #编译安装nginxshell: cd /usr/local/nginx-1.9.6 ./configure --prefix/usr/local/nginx
- name: Make Nginx #初始化nginxshell: cd /usr/local/nginx-1.9.6 make make install
- name: Copy Nginx Start Script #复制nginx启动脚本template: srcnginx dest/etc/init.d/nginx ownerroot grouproot mode0755
- name: Copy Nginx Config #复制nginx配置文件template: srcnginx.conf dest{{ nginx_basedir }}/conf/ ownerroot grouproot mode0644 在roles/install/tasks/install.yml下编写启动文件
- name: create nginx useruser: name{{ nginx_user }} statepresent createhomeno shell/sbin/nologin
- name: start nginx serviceshell: /etc/init.d/nginx start
- name: add boot start nginx serviceshell: chkconfig --level 345 nginx on
- name: delete nginx compression filesshell: rm -rf /tmp/nginx-1.9.6.tar.gz
在 roles/install/tasks/main.yml中编写主要文件
- include: copy.yml
- include: install.yml
执行脚本结果如图所示 在test主机中查看nginx是否启动 可以看到启动成功
案例二管理nginx配置文件
首先创建如下目录 roles目录下分为new目录和old目录new目录表示需要更新的配置文件old目录为备份的文件。在做配置文件的时候备份非常重要在执行update.yml文件前应该备份好当前配置文件出现错误时可以及时的回滚操作。 rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/
使用该命令可以将new目录中的配置文件备份到old目录中
files目录中表示的是配置文件可以自行添加此处放置的为空目录。
handlers目录中表示的是重启服务内容如下
- name: restart nginx #用于重新加载nginx服务shell: /etc/init.d/nginx reload
tasks目录中就是主要的命令内容如下
- name: copy conf file #复制.conf和hosts文件copy: src{{ item.src }} dest{{ nginx_basedir }}/{{ item.dest }} backupyes ownerroot grouproot mode0644with_items:- { src: nginx.conf, dest: conf/nginx.conf }- { src: vhosts, dest: conf/ }notify: restart nginx
vars目录中表示变量内容如下
nginx_basedir: /usr/local/nginx #定义变量
update.yml文件为更新文件内容为
---
- hosts: test #入口文件user: rootroles:- new #这里只有new
执行命令结果如下 由于此处的files目录中放置的是空目录所以结果没有任何改变可以自行添加配置文件进行修改。