jenkins+ansible+gitlab自动化部署nginx

本文介绍如何使用Jenkins、GitLab和Ansible进行自动化部署Nginx的过程,包括环境配置、云主机设置、域名解析、Ansible playbooks创建、YAML执行文件编写、inventory变量设定、模板文件设计等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境信息:

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

duan737174646

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值