目录结构
[root@master tar_install_nginx]# tree
.
├── nginx-1.22.1.tar.gz
├── nginx_install_tar.yaml
└── nginx.service
文件解释
nginx-1.22.1.tar.gz
nginx安装包,其他版本也可以
nginx.service
为了设置系统启动(systemctl start nginx)等
具体文件内容
[Unit]
Description=nginx web service
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
nginx_install_tar.yaml
自定义yum文件
- hosts: node2
tasks:
- name: 安装编译环境和依赖包
yum:
name:
- gcc
- gcc-c++
- pcre
- pcre-devel
- zlib
- zlib-devel
- openssl
- openssl-devel
- name: 复制解压nginx安装包
unarchive:
src: ~/ansible/tar_install_nginx/nginx-1.22.1.tar.gz
dest: ~/
- name: 创建文件夹
file:
path: /usr/local/nginx
state: directory
- name: 安装nginx至/usr/local/nginx
shell: cd /root/nginx-1.22.1;./configure --prefix=/usr/local/nginx && make && make install
- name: 编写系统启动脚本
template:
src: nginx.service
dest: /usr/lib/systemd/system/
- name: 启动nginx服务
service:
name: nginx
state: started
enabled: yes
- name: 打开80端口
firewalld:
port: 80/tcp
permanent: yes
immediate: yes
state: enabled
检查yml文件格式是否正确,没有报错则没问题
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install_tar.yaml --syntax-check
playbook: nginx_install_tar.yaml
查看操作哪些机器节点
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install_tar.yaml --list-hosts
playbook: nginx_install_tar.yaml
play #1 (node2): node2 TAGS: []
pattern: [u'node2']
hosts (1):
node2
执行,注意yaml文件没有和主机清单文件在同一目录,需要指定主机清单文件路径
[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install_tar.yaml