环境信息:
jenkins + gitlab + ansible 一台云主机 4u8g 域名:www.gitlib.com 在hosts 里定义
要部署nginx的机器 配置 4u4g 云主机, 域名:www.bbb.com 在hosts 里定义
ssh 的端口为 2222
nginx_job界面配置:
命令行:
目录结构
一、创建nginx_playbooks项目
mkdir nginx_playbooks
cd nginx_playbooks
mdkir {inventory,roles} -p
二、创建yml执行文件
vim deploy.yml
- hosts: "www.bbb.com"
gather_facts: true
remote_user: root
roles:
- nginx
三、创建inventory变量
cd inventory
vim dev
[nginx]
www.bbb.com ansible_ssh_user="root" ansible_ssh_port=2222
[nginx:vars]
server_name=www.bbb.com
port=80
user=duan
worker_processes=4
max_open_file=65505
root=/www
vim prod
[nginx]
www.bbb.com
ansible_ssh_user="root"
ansible_ssh_port=2222
[nginx:vars]
server_name=www.bbb.com
port=80
user=duan
worker_processes=4
max_open_file=65505
root=/www
roles 目录结构
mkdir nginx
mkdir nginx{files,tasks,templates} -p
四、创建检测状态脚本
vim health_check.sh
#!/bin/sh
URL=$1
curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"
五、创建访问界面
vim index.html
This is my first website
六、创建main工作文件
vim main.yml
- name: Disable system firewall
service: name=firewalld state=stopped
- name: Disable SELINUX
selinux: state=disabled
- name: setup nginx yum source
yum: pkg=epel-release state=latest
- name: install nginx
yum: pkg=nginx state=installed
- name: write then nginx config file
template: src=roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: create nginx root folder
file: 'path={{ root }} state=directory owner={{ user }} group={{ user }} mode=0755'
- name: copy index.html to remote
copy: 'remote_src=no src=roles/nginx/files/index.html dest=/www/index.html mode=0755'
- name: copy health_check.sh to remote
copy: 'remote_src=no src=roles/nginx/files/health_check.sh dest=/www/health_check.sh mode=0755'
- name: restart nginx service
service: name=nginx state=restarted
- name: run the health check locally
shell: "sh /www/health_check.sh {{ server_name }}"
delegate_to: www.bbb.com
register: health_status
- debug: msg="{{ health_status.stdout }}"
七、创建模板文件
vim nginx.conf.j2
# For more information on configuration, see:
user {{ user }};
worker_processes {{ worker_processes }};
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections {{ max_open_file }};
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
#include /etc/nginx/conf.d/*.conf;
server {
listen {{ port }} default_server;
server_name {{ server_name }};
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root {{ root }};
index index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
开始构建。
访问www.bbb.com, 本机不能访问,在 windows的hosts里加解析。
成功,success。