1. 程式人生 > 其它 >ansible實現編譯部署nginx

ansible實現編譯部署nginx

使用ansible編譯部署nginx,通過template模板修改配置檔案及建立虛擬機器;
大概流程如下:
虛擬機器埠號定義;預安裝依賴包;使用者 組 下載包 解壓 編譯 service檔案;子配置資料夾;安裝目錄許可權修改;模板主配置檔案,更新觸發重啟;模板子配置檔案,更新觸發重啟;index網頁檔案;handler重啟;

---
- hosts: webser
  remote_user: root
  gather_facts: yes
  vars:
    vhosts:
    - 81
    - 88

  tasks:
  - name: preinstall
    yum: name={{ item }} state=present
    with_items:
    - pcre
    - pcre-devel
    - openssl
    - openssl-devel
    - zlib
    - zlib-devel
    - gcc-c++

  - name: create group
    group: name=nginx system=yes

  - name: create user
    user: name=nginx group=nginx shell=/sbin/nologin system=yes

  - name: download nginx's tarball
    get_url: url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src

  - name: unarchive tarball
    unarchive: remote_src=yes src=/usr/local/src/nginx-1.18.0.tar.gz dest=/usr/local/src

  #- name: install nginx
  #  make:
  #   chdir: /usr/local/src/nginx-1.18.0
  #   target: install
  #   file: /etc/ansible/Makefile 
  #   params:
  #     PREFIX: /data/nginx/ 

  - name: install nginx
    shell: chdir=/usr/local/src/nginx-1.18.0 ./configure --prefix=/data/nginx;make; make install

  - name: create service file
    copy: src=nginx.service dest=/usr/lib/systemd/system/
    notify: systemctl reload

  - name: create sub config dir
    file: name=/data/nginx/conf.d/ state=directory

  - name: chown /data/nginx
    file: path=/data/nginx owner=nginx group=nginx recurse=yes

  - name: use template nginx.conf.j2 for new main config and backup old config
    template: src=nginx.conf.j2 dest=/data/nginx/conf/nginx.conf backup=yes
    notify: restart nginx

  - name: use template virtual_host.conf.j2 to create virtualhost
    template: src=virtual_host.conf.j2 dest=/data/nginx/conf.d/virtual_host.conf
    notify: restart nginx

  - name: copy index.html
    copy: src=index.html dest=/data/nginx/html/index.html backup=yes

  handlers:
  - name: restart nginx
    service: name=nginx  

  - name: systemctl reload
    shell: systemctl reload