Ansible剧本-基于yaml文件实现nginx源码安装

基于yaml文件实现nginx源码安装

主机规划
  • ansible:192.168.31.63
  • 被管理1:192.168.31.64
  • 被管理2:192.168.31.65
基于yaml文件实现如下功能
  • 在64和65两个主机上都安装nginx
  • 在64上的nginx工作在81端口
  • 在65上的nginx工作在82端口

准备操作

在ansible主机,安装epel源

[root@centos6-1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

在ansible主机,安装nginx

[root@centos6-1 ~]# yum install nginx -y

在ansible主机,基于nginx的配置文件生成配置文件模板

[root@centos6-1 ~]# cp /etc/nginx/nginx.conf.default /root/template.j2
[root@centos6-1 ~]# vim /root/template.j2
# 修改一行
listen       {{ nginx_port }};

修改主机清单文件

[root@centos6-1 ~]# vim /etc/ansible/hosts 
[webservers]
192.168.31.64  nginx_port=81
192.168.31.65  nginx_port=82

yaml文件

[root@localhost ~]# vim nginx_install.yaml
- hosts: all
  tasks:
  - name: delete old epel repo file
    shell: rm -rf /etc/yum.repos.d/epel*
  - name: create new epel repo file
    shell: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  - name: install nginx
    yum: name=nginx state=latest
  - name: send nginx.conf
    template: src=/root/template.j2 dest=/etc/nginx/nginx.conf
    notify:
    - restart nginx
  - name: start nginx
    service: name=nginx state=started enabled=true
  handlers:
  - name: restart nginx
    service: name=nginx state=restarted
# 执行yaml文件
[root@localhost ~]#  ansible-playbook nginx_install.yaml
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值