写一个ansible 一键部署nginx的yaml脚本

---
- hosts: web
  remote_user: root
  ##gather_facts: no执行playbook时默认收集目标主机信息。禁用可以提高效率
  gather_facts: no 
  #配置环境变量
  vars:
    nginx_dowload_url: https://nginx.org/download/nginx-1.25.2.tar.gz
    src_nginx_dir: /usr/src/nginx-1.25.2
    nginx_install_dir: /usr/local/nginx
    nginx_jieyahou_name: nginx-1.25.2
  tasks:
    - name: install deployment huanjing
      yum: name=gcc,gcc-c++,make,zlib-devel,pcre-devel,openssl-devel state=installed
    - name: download nginx    
      get_url: url={{ nginx_dowload_url }} dest=/usr/src  #下载nginx到/tmp目录下
    - name: tar nginx
      shell: cd /usr/src;tar -xf nginx-1.25.2.tar.gz ##解压nginx包
    - name: create nginx  #创建nginx用户
      #判断用户是否存在,存在也继续执行
      shell: if [ `useradd nginx -M -s /sbin/nologin`  ];then continue;fi
    - name: install nginx    #编译安装nginx
      shell: cd {{ src_nginx_dir }};./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-stream && make -j 4 && make install
    - name: backup nginx conf file 
      shell: cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bac
    - name: cp conf file
      #把想要修改的配置文件放在/root/下
      copy: src=/root/nginx.conf dest=/usr/local/nginx/conf/
      notify: copy conf file
    - name: start nginx
      shell: /usr/local/nginx/sbin/nginx
  handlers:
    - name: copy conf file
      shell: /usr/local/nginx/sbin/nginx -s reload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值