微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐