ansible 的role 实现 nginx 编译安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #文件目录结构 [root@centos8 ansible ] #tree . ├── role_nginx.yml └── roles └── nginx ├── files │ ├── nginx.conf │ └── nginx.service ├── tasks │ ├── configure.yml │ ├── config.yml │ ├── group.yml │ ├── link.yml │ ├── main.yml │ ├── make .yml │ ├── package.yml │ ├── run_dir.yml │ ├── service_file.yml │ ├── start_serivce.yml │ ├── unarchive.yml │ └── user.yml └── vars └── main.yml 5 directories, 16 files #config文件 [root@centos8 ansible ] #grep -Ev "#|^$" roles/nginx/files/nginx.conf user nginx nginx; worker_processes 1; pid run /nginx .pid; events { worker_connections 1024; } http { include mime.types; default_type application /octet-stream ; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } } } #service文件 [root@centos8 ansible ] #vim roles/nginx/files/nginx.service [Unit] Description=nginx - high performance web server Documentation=http: //nginx .org /en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile= /apps/nginx/run/nginx .pid ExecStart= /usr/sbin/nginx -c /apps/nginx/conf//nginx .conf ExecReload= /bin/sh -c "/bin/kill -s HUP $(/bin/cat /apps/nginx/run/nginx.pid)" ExecStop= /bin/sh -c "/bin/kill -s TERM $(/bin/cat /apps/nginx/run/nginx.pid)" [Install] WantedBy=multi-user.target #vars文件 [root@centos8 ansible ] #cat roles/nginx/vars/main.yml --- nginx_verison: nginx-1.18.0 suffix: . tar .gz down_dir: /usr/local/src/ ins_dir: /apps/nginx/ #tasks文件 [root@centos8 ansible ] #cat roles/nginx/tasks/*.yml - name: configure shell: . /configure \ --prefix={{ ins_dir }} \ --user=nginx --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module args: chdir: "{{ down_dir }}{{ nginx_verison }}" - name: config copy: src=nginx.conf dest={{ ins_dir }} /conf/nginx .conf - name: group nginx group: name=nginx state=present system= yes - name: link file file : src: "{{ ins_dir }}sbin/nginx" dest: /usr/sbin/nginx state: link - name: make shell: make -j "{{ ansible_processor_vcpus }}" && make install args: chdir: "{{ down_dir }}{{ nginx_verison }}/" - name: nginx dependence packages #安装centos8 nginx依赖 yum: name: - gcc - make - pcre-devel - openssl-devel - zlib-devel state: present - name: run dir create file : path= "{{ ins_dir }}run" owner=nginx group=nginx state=directory <br>- name: service file copy: src=nginx.service dest= /usr/lib/systemd/system/nginx .service - name: start serivce service: name=nginx state=started enabled= yes - name: unarchive #下载解压nginx包到目标主机 unarchive: src: "http://nginx.org/download/{{ nginx_verison }}{{ suffix }}" dest: "{{ down_dir }}" remote_src: yes - name: user nginx user: name: nginx shell: /sbin/nologin system: yes group: nginx home: /home/nginx create_home: no [root@centos8 ansible ] #cat roles/nginx/tasks/main.yml --- - include: package.yml - include: group.yml - include: user.yml - include: unarchive.yml - include: configure.yml - include: make .yml - include: link.yml - include: service_file.yml - include: config.yml - include: run_dir.yml - include: start_serivce [root@centos8 ansible ] #cat role_nginx.yml --- - hosts: 192.168.6.18 remote_user: root roles: - nginx |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话