ansible 的role 实现 nginx 编译安装

#文件目录结构
[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

- 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

  

posted @ 2022-04-13 10:48  辛杨  阅读(65)  评论(0)    收藏  举报