Ansible使用playbook批量安装Nginx

博观而约取,厚积而薄发。这篇文章主要讲述Ansible使用playbook批量安装Nginx相关的知识,希望能为你提供帮助。
在Ansible主机上定义hosts文件和准备安装包,目录结构如下:

hosts里面定义主机:(这里仅写一台作为示例)

install.yml:

- hosts: test
tasks:
- name: 安装依赖包
yum: name=zlib,zlib-devel,openssl,openssl-devel,pcre,pcre-devel,gcc state=present
- name: 传输并解安装包
unarchive: src=https://www.songbingjia.com/android/nginx-1.21.3.tar.gz dest=/usr/local/ copy=yes
- name: 编译安装
shell: cd /usr/local/nginx-1.21.3 & & ./configure & & make & & make install
- name: 配置自启动文件
copy: src=https://www.songbingjia.com/android/nginx.service dest=/lib/systemd/system/
- name: 添加自启动
service: name=nginx state=started enabled=no

说明:
hosts:指定要安装的主机组
tasks:安装详细步骤(示例Nginx采用最小化安装)
nginx.service:添加systemctl管理服务
[UNIT]
Deion=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=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target



执行安装:
1.测试主机连通性:

2.执行install.yml

语法检查无报错

安装成功

检查目标机器上的nginx进程和80端口正常
【Ansible使用playbook批量安装Nginx】


    推荐阅读