ansible playbook批量部署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’ 的模式来实现。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值