基于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