当你拿到几十台服务器都要安装openresty的时候是不是懵了,不要怕,下面教你一键部署,前提是,你得安装Ansible,如果不会Ansible,关注我,带你从入门到放弃。
安装前需准备文件:
openresty-1.17.8.2.tar.gz
openssl-1.1.1h.tar.gz
pcre-8.44.tar.gz
perl-5.32.0.tar.gz
zlib-1.2.11.tar.gz
openresty.service
以下是ansible-playbook的剧本文件,保存为:install_openresty.yml
---
- name: install openresty
gather_facts: False
hosts: "{{ ip }}"
user: root
tasks:
- name: Create the dir
file: path={{ item }} state=directory
with_items:
- /opt/openresty/
- name: copy package
copy: src={{ item.src }} dest={{ item.dest }} mode='0755'
with_items:
- {src: "openresty-1.17.8.2.tar.gz",dest: "/opt/openresty/"}
- {src: "openssl-1.1.1h.tar.gz",dest: "/opt/openresty/"}
- {src: "perl-5.32.0.tar.gz",dest: "/opt/openresty/"}
- {src: "pcre-8.44.tar.gz",dest: "/opt/openresty/"}
- {src: "zlib-1.2.11.tar.gz",dest: "/opt/openresty/"}
- {src: "openresty.service",dest: "/usr/lib/systemd/system/"}
- name: unarchive openresty
unarchive: src=/opt/openresty/openresty-1.17.8.2.tar.gz dest=/opt/openresty/ copy=no
- name: unarchive openssl
unarchive: src=/opt/openresty/openssl-1.1.1h.tar.gz dest=/opt/openresty/ copy=no
- name: unarchive pcre
unarchive: src=/opt/openresty/pcre-8.44.tar.gz dest=/opt/openresty/ copy=no
- name: unarchive zlib
unarchive: src=/opt/openresty/zlib-1.2.11.tar.gz dest=/opt/openresty/ copy=no
- name: unarchive perl
unarchive: src=/opt/openresty/perl-5.32.0.tar.gz dest=/opt/openresty/ copy=no
- name: install gcc
shell: yum -y install gcc gcc-c++ zlib
- name: install perl
shell: cd /opt/openresty/perl-5.32.0 && ./Configure -de && make && make install
ignore_errors: yes
- name: install openresty
shell: cd /opt/openresty/openresty-1.17.8.2 && ./configure --prefix=/usr/local/openresty --with-zlib=/opt/openresty/zlib-1.2.11 --with-pcre=/opt/openresty/pcre-8.44 --with-stream --with-openssl=/opt/openresty/openssl-1.1.1h && make && make install
ignore_errors: yes
- name: start service
shell: systemctl start openresty
将openresty配置为服务,以下是openresty.service内容
[Unit] Description=Openrestry daemon [Service] Type=forking ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
准备好脚本后,执行命令
ansible-playbook install_openresty.yml -e "ip"="" 注:参数 ip 所带的内容为ansible配置的hosts中的主机分组或者ip



