ansible批量部署多台nginx

1.环境部署

使用三台机器:
ansible-server: 192.168.152.129
ansible-client: 192.168.152.135
ansible-client1: 192.168.152.136
首相做好/etc/hosts本地解析,以及/etc/ansible/hosts 的分组,将安装ansible的机器的公钥拷贝到其他手控制的机器上;
roles在ansible中,playbooks的目录组织结构;模块化之后,成为roles的组织结构,易读,代码可重用,层次清晰
ansible的目录结构
在这里插入图片描述

目录顺序:
role_name/     ---角色名称=目录
    files/:存储一些可以用copy调用的静态文件。
    tasks/: 存储任务的目录,此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用; 
    handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由(与notify:名字相同,方便notify通知执行下一条命令)通过main.yml进行“包含”调用; 
    vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用; 
    templates/:存储由template模块调用的模板文本; (也可以调用变量)
    site.yml:定义哪个主机应用哪个角色
[root@ansible-server ~]# cd /etc/ansible/roles/  #role为自带目录,如果不存在可以创建
[root@ansible-server roles]# mkdir nginx/{files,handlers,tasks,templates,vars} -p
2.创建文件:
[root@ansible-server roles]# touch site.yml nginx/{handlers,tasks,vars}/main.yml
[root@ansible-server roles]# yum install -y tree

在这里插入图片描述

2.开始批量部署nginx

注:以下没有特别说明,则都是在ansible-server机器上操作

[root@ansible-server roles] yum -y epel-release
yum -y install nginx
[root@localhost roles]# touch site.yaml
1.创建nginx的测试文件
[root@localhost ]# cd /etc/ansible/roles/nginx/files   //以下操作在files目录下
vim index.html    //为显示与nginx不同的页面
this is the page of nginx    //编写页面后wq保存退出
2.安装nginx并配置模板
[root@localhost ]# cd /etc/ansible/roles/nginx/templates   //以下操作在templates目录下
cp /etc/nginx/nginx.con ./
mv nginx.conf nginx.conf.j2
vim nginx.conf.j2
 #user  nobody;
worker_processes  {{ cpu }};    //修改自定义变量

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

在这里插入图片描述

3.编写任务
[root@localhost ]# cd /etc/ansible/roles/nginx/tasks    //以下操作在tasks目录下 
vim main.yml
---
- name: install epel
  yum: name=epel-release state=latest
- name: install nginx
  yum: name=nginx state=latest
- name: copy config file
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx
- name: copy index.html
  copy: src=/etc/ansible/roles/nginx/files/index.html dest=/usr/share/nginx/html/index.html backup=yes

在这里插入图片描述

4.编写变量
[root@localhost ]# cd /etc/ansible/roles/nginx/vars   //以下操作在vars目录下
vim main.yml
cpu: 1

在这里插入图片描述

5.编写handlers
[root@localhost ]# cd /etc/ansible/roles/nginx/handlers   //以下操作在handlers目录下
vim main.yml
---
- name: restart nginx
  service: name=nginx state=started

在这里插入图片描述

6.编写剧本
[root@localhost]# cd /etc/ansible/roles   //以下操作在roles目录下
vim site.yml
---
- hosts: server
  user: root
  roles:
  - nginx

在这里插入图片描述

7.检测语法:
[root@localhost roles]# ansible-playbook --syntax-check site.yaml 

playbook: site.yaml

8.执行剧本:
[root@localhost roles]# ansible-playbook site.yaml 

PLAY [server] ***********************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.152.135]
ok: [192.168.152.136]

TASK [nginx : install epel] *********************************************************************************************************
ok: [192.168.152.136]
ok: [192.168.152.135]

TASK [nginx : install nginx] ********************************************************************************************************
ok: [192.168.152.136]
ok: [192.168.152.135]

TASK [nginx : copy config file] *****************************************************************************************************
ok: [192.168.152.136]
ok: [192.168.152.135]

TASK [nginx : copy index.html] ******************************************************************************************************
changed: [192.168.152.135]
changed: [192.168.152.136]

PLAY RECAP **************************************************************************************************************************
192.168.152.135            : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.152.136            : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

在这里插入图片描述
配置成功后可在两台机器查看nginx是否已安装,浏览器页面测试访问:http://ip

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值