ansible实战__使用安装包安装nginx

目录结构

[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.yaml --syntax-check

playbook: nginx_install.yaml

查看操作哪些机器节点

[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install.yaml --list-hosts

playbook: nginx_install.yaml

  play #1 (node2): node2        TAGS: []
    pattern: [u'node2']
    hosts (1):
      node2

执行,注意yaml文件没有和主机清单文件在同一目录,需要指定主机清单文件路径

[root@master yum_install_nginx]# ansible-playbook -i ../hosts nginx_install.yaml
posted @   yohoo随便玩玩  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示