ansible-playbook编译安装nginx

git地址

https://github.com/ledrsnet/my-ansible-example/tree/main/compile_nginx

目录结构

[root@maple-c8-n1 ansible]# tree
.
├── ansible.cfg
├── compile_nginx.yml
├── hosts
└── roles
    └── nginx
        ├── files
        ├── handlers
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   ├── compile.yml
        │   ├── main.yml
        │   ├── postCompile.yml
        │   └── preCompile.yml
        ├── templates
        │   └── nginx.service.j2
        └── vars
            └── main.yml

自定义变量

[root@maple-c8-n1 ansible]# cat roles/nginx/vars/main.yml 
---
# vars file for nginx
pk_dir: /usr/local/src/
install_path: /apps/nginx/
pid_dir: "{{ install_path }}/run/"
packages:
  - https://nginx.org/download/nginx-1.18.0.tar.gz
filenames:
  nginx: "{{ packages[0].partition('.tar')[0]|basename }}"

task文件

#主task文件
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/main.yml 
---
# tasks file for nginx
- include: preCompile.yml
- include: compile.yml
- include: postCompile.yml

#preCompile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/preCompile.yml 
- name: install gcc..
  yum: name="gcc,pcre-devel,openssl-devel,zlib-devel" state=present
  when: ansible_distribution == "CentOS"
- name: apt install packages
  apt: name="gcc,make,libapr1-dev,libaprutil1-dev,libpcre3,libpcre3-dev,libssl-dev,wget"
  when: ansible_distribution != "CentOS"
- name: add user nginx
  user: name=nginx shell=/sbin/nologin create_home=no
- name: download nginx src packages
  unarchive: src={{ packages[0] }} dest={{ pk_dir}} copy=no

#compile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/compile.yml 
- name: configure scripts
  shell: 
    chdir: "{{ pk_dir }}{{ filenames.nginx }}"
    cmd: ./configure --prefix={{ install_path }} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
- name: compile nginx
  make: 
    chdir: "{{ pk_dir }}{{ filenames.nginx }}"
    target: install
    file: "{{ pk_dir }}{{ filenames.nginx }}/Makefile"

#postCompile.yml
[root@maple-c8-n1 ansible]# cat roles/nginx/tasks/postCompile.yml 
- name: change app owner
  file: path={{ install_path }} owner=nginx group=nginx recurse=true
- name: create soft link
  file: src={{ install_path }}/sbin/nginx dest=/usr/sbin/nginx state=link
- name: copy nginx service file
  template: src=nginx.service.j2 dest=/usr/lib/systemd/system/nginx.service
  when: ansible_distribution == "CentOS"
- name: copy nginx service file
  template: src=nginx.service.j2 dest=/lib/systemd/system/nginx.service
  when: ansible_distribution != "CentOS"
- name: mkdir pid dir
  file: path={{ pid_dir }} state=directory
- name: modify pid path in nginx.conf
  lineinfile:
    path: "{{ install_path }}/conf/nginx.conf"
    regexp: '^#pid'
    line: "pid {{ pid_dir }}/nginx.pid;"
- name: reload service daemon
  shell: systemctl daemon-reload
- name: start Service
  service: name=nginx state=started enabled=yes

模板文件

[root@maple-c8-n1 ansible]# cat roles/nginx/templates/nginx.service.j2 
[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={{ install_path }}/run/nginx.pid
ExecStart={{ install_path }}/sbin/nginx -c {{ install_path }}/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target

总调度文件

[root@maple-c8-n1 ansible]# cat compile_nginx.yml 
---
- hosts: web
  
  roles:
    - nginx
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清枫cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值