系统相关
首页 > 系统相关> > ansible实现编译部署nginx

ansible实现编译部署nginx

作者:互联网

使用ansible编译部署nginx,通过template模板修改配置文件及创建虚拟机;

大概流程如下:

虚拟机端口号定义;预安装依赖包;用户 组 下载包 解压 编译 service文件拷贝;子配置文件夹创建;安装目录权限修改;模板主配置文件,更新触发重启;模板子配置文件,更新触发重启;index网页文件;handler重启;

需准备如下4个文件:

nginx.service index.html templates/nginx.conf.j2 templates/virtual_host.conf.j2

[root@17 ansible]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── index.html
├── install_nginx.yml
├── nginx.service
├── roles
└── templates
    ├── nginx.conf.j2
    └── virtual_host.conf.j2

2 directories, 7 files

playbook install_nginx.yml如下:

---
- 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

结果测试如下:

[root@17 ansible]# curl 10.0.0.27
80 81 88
[root@17 ansible]# curl 10.0.0.27:81
80 81 88
[root@17 ansible]# curl 10.0.0.27:88
80 81 88

标签:src,name,编译,j2,nginx,ansible,conf
来源: https://www.cnblogs.com/windman0/p/15368464.html