ansible playbook批量部署nginx

Ansible批量部署Nginx
本文介绍如何使用Ansible Playbook批量在多台Linux服务器上部署Nginx软件,包括复制并解压安装包、源码安装、创建可执行文件链接及启动Nginx服务。同时提供错误记录及常见操作命令。

ansible playbook批量部署nginx软件

规划

软件安装包:nginx-1.16.1.tar.gz
源文件安装存放目录:/install/nginx-1.16.1.tar.gz
nginx安装目录:/usr/local/nginx
nginx解压缩目录:/usr/local/src
解压之后的源文件目录为/usr/local/src/nginx-1.16.1

部署环境:

操作中系统ip主机名软件安装目录
Linux7.5192.168.174.129srvb/usr/local/nginx
Linux7.5192.168.174.130srvc/usr/local/nginx
Linux7.5192.168.174.131srvd/usr/local/nginx

程序部署思路说明:

1、通过将存放在ansible管理端的/install/nginx-1.16.1.tar.gz,复制并解压到受管端/usr/local/src/nginx-1.16.1目录
2、进入受管端/usr/local/src/nginx-1.16.1,进行nginx源码安装
3、安装完成之后,进行添加到系统命令行,也就是可执行文件link到/usr/sbin中
4、启动nginx

nginx.yaml配置文件

---
- hosts: srv
  vars: 
    - srcdir: nginx-1.16.1
    - insdir: /usr/local/nginx
  remote_user: root
  tasks: 
    - name: cp and untar
      unarchive: src=/install/nginx-1.16.1.tar.gz dest=/usr/local/src
    - name: compile and install nginx
      shell : cd /usr/local/src/{{ srcdir }}; ./configure --prefix={{ insdir }} --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module && make && make install
    - name: link the command
      file : src={{ insdir }}/sbin/nginx dest=/usr/sbin/nginx state=link
    - name: start nginx
      shell : nginx

执行安装:
ansible-playbook nginx.yaml

**部署记录:**

[root@srva yaml]# ansible-playbook nginx.yaml 
 [WARNING]: Found both group and host with same name: srva

PLAY [srv] ************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************
ok: [srvc]
ok: [srvd]
ok: [srvb]

TASK [cp and untar] ***************************************************************************************************
changed: [srvb]
changed: [srvc]
changed: [srvd]

TASK [compile and install nginx] **************************************************************************************
changed: [srvd]
changed: [srvb]
changed: [srvc]

TASK [link the command] ***********************************************************************************************
changed: [srvc]
changed: [srvb]
changed: [srvd]

TASK [start nginx] ****************************************************************************************************
changed: [srvc]
changed: [srvb]
changed: [srvd]

PLAY RECAP ************************************************************************************************************
srvb                       : ok=5    changed=4    unreachable=0    failed=0   
srvc                       : ok=5    changed=4    unreachable=0    failed=0   
srvd                       : ok=5    changed=4    unreachable=0    failed=0   

[root@srva yaml]# 

补充说明

批量创建安装目录:
ansible srv -m shell -a ‘mkdir -p /usr/local/nginx’ 注:也可以使用file模块来处理
批量关闭nginx:
ansible srv -m shell -a ‘nginx -s stop’
查看nginx进程:
ansible srv -m shell -a 'ps -ef|grep nginx|grep -v grep ’

错误记录:
在配置yaml文件时,我们使用shell模块,又使用chdir来改变目录。这个是不正确的。
chdir是在command模块带的切换目录参数。而shell模块没有这个。
shell和raw模块要切换目录,可以使用 -a ‘cd /usr/local/nginx/sbin; ./nginx -s start’ 的模式来实现。

### 使用 Ansible Playbook 安装 Nginx 的案例 以下是基于多个引用内容整理的一个完整的 Ansible Playbook 示例,用于安装和配置 Nginx: #### 1. 准备工作 确保已经正确配置了 Ansible 环境,并且目标主机已添加到 `hosts` 文件中。 ```yaml --- - name: Install and Configure Nginx hosts: web become: true tasks: - name: Ensure required packages are installed yum: name: "{{ item }}" state: present loop: - gcc - make - openssl-devel - pcre-devel - zlib-devel - wget tags: preparation - name: Download Nginx source package shell: | cd /tmp/ wget https://nginx.org/download/nginx-1.21.6.tar.gz register: download_result failed_when: "'404 Not Found' in download_result.stdout" changed_when: "'saved' in download_result.stderr or 'already exists' not in download_result.stderr" tags: download - name: Extract the downloaded archive unarchive: src: /tmp/nginx-1.21.6.tar.gz dest: /tmp/ remote_src: yes tags: extraction - name: Compile and install Nginx from source shell: | cd /tmp/nginx-1.21.6 ./configure --prefix=/usr/share/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/run/nginx.pid \ --with-http_ssl_module make && make install args: creates: /usr/share/nginx/sbin/nginx tags: compilation - name: Create necessary directories for logs file: path: "/var/log/nginx/{{ item }}" state: directory mode: '0755' with_items: - access.log - error.log notify: restart nginx tags: log_directories - name: Copy custom configuration files to target host copy: src: /path/to/local/nginx.conf dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' notify: reload nginx tags: config_copy - name: Start and enable Nginx service systemd: name: nginx state: started enabled: yes tags: service_management handlers: - name: Restart Nginx systemd: name: nginx state: restarted - name: Reload Nginx Configuration systemd: name: nginx state: reloaded ``` 此脚本涵盖了从依赖包安装、源码下载编译到最终服务启动的全过程[^3]。如果只需要通过 Yum 或 Apt 包管理器快速安装,则可以简化为以下版本[^4]: ```yaml --- - name: Quick Installation of Nginx via Package Manager hosts: web become: true tasks: - name: Install Nginx using YUM (for CentOS/RHEL) yum: name: nginx state: present - name: Start and Enable Nginx Service service: name: nginx state: started enabled: yes ``` 以上两种方法均可实现 Nginx 的安装与运行,具体选择取决于实际需求以及环境条件。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值