---
- 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
写一个ansible 一键部署nginx的yaml脚本
最新推荐文章于 2023-10-13 16:36:14 发布