ansible一键安装nginx二进制版本

公司需求需要批量部署nginx,闲来无事,改进了原先手动部署的方式,写了一个ansible-playbook

复制代码
- hosts: web
  gather_facts: yes
  vars:
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
  tasks:
   - name: add group
     group:
         name: nginx
         state: present

   - name: add user
     user:
         name: nginx
         state: present
         group: nginx
         shell: /sbin/nologin
   - name: check path
     stat:
         path: /usr/local/nginx
     register: file_stat
   - name: unarchive
     unarchive:
         src: /etc/ansible/files/nginx/nginx.tar.gz
         dest: /usr/local/
         owner: nginx
         group: nginx
     when: file_stat.stat.exists == False

   - name: check path
     stat:
         path: /apps/nginx/conf
     register: file_stat_nginx
   - name: compile and install
     shell:
         cmd: "./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module &&  make && make install"
         chdir: "/usr/local/nginx"
     when: file_stat_nginx.stat.exists == False
   - name: chown
     shell:
         cmd: "chown -R  nginx.nginx /apps/nginx && ln -sv /apps/nginx/sbin/nginx /usr/sbin/nginx"
     ignore_errors: yes
   - name: mkdir  pid
     file:
         path: /apps/nginx/run
         state: directory
         owner: nginx
         group: nginx
         recurse: yes
   - name: conf file
     template:
         src: /etc/ansible/files/nginx/nginx.conf
         dest: /apps/nginx/conf/nginx.conf
         owner: nginx
         group: nginx

   - name: mkdir  pid
     file:
         path: /apps/nginx/conf.d
         state: directory
         owner: nginx
         group: nginx
         recurse: yes
   - name: conf.d  file
     template:
         src: /etc/ansible/files/nginx/bbs.com.conf
         dest: /apps/nginx/conf.d/bbs.com.conf
         owner: nginx
         group: nginx

   - name: service file
     template:
         src: /etc/ansible/files/nginx/nginx.service
         dest: /lib/systemd/system/nginx.service
     changed_when: False
   - name: start nginx
     service:
         name: nginx
         state: restarted
         enabled: yes

附加文件

bbs.com.conf

复制代码
[root@devops nginx]#cat bbs.com.conf
server {
   listen       80;
   server_name  bbs.com;
   root /apps/nginx/html;
}




[root@devops nginx]#cat nginx.conf
worker_processes  1;
pid        /apps/nginx/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include /apps/nginx/conf.d/*.conf;
}

添加service文件,方便后续管理

复制代码
[root@devops nginx]#cat nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=10000

[Install]
WantedBy=multi-user.targe
相关推荐
m0_6948455720 分钟前
服务器如何配置防火墙规则开放/关闭端口?
linux·服务器·安全·云计算
阿巴~阿巴~1 小时前
Linux基本命令篇 —— alias命令
linux·服务器·bash
好名字更能让你们记住我2 小时前
Linux多线程(十二)之【生产者消费者模型】
linux·运维·服务器·jvm·windows·centos
门思科技2 小时前
设计可靠 LoRaWAN 设备时需要考虑的关键能力
运维·服务器·网络·嵌入式硬件·物联网
小锋学长生活大爆炸2 小时前
【知识】RPC和gRPC
服务器·网络协议·rpc
学习编程的gas2 小时前
Linux开发工具——gcc/g++
linux·运维·服务器
大大。2 小时前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
_可乐无糖2 小时前
AWS WebRTC: 判断viewer端拉流是否稳定的算法
linux·服务器·webrtc·aws
老友@3 小时前
服务器异常宕机或重启导致 RabbitMQ 启动失败问题分析与解决方案
服务器·rabbitmq·启动失败·宕机
程序猿追3 小时前
免费版安全性缩水?ToDesk、TeamViewer、向日葵、网易UU远程访问&隐私防护测评
服务器·网络·科技·teamviewer