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