PlayBook
1.目录结构
[root@ansible ~]# tree roles/
roles/
├── nginx
│?? ├── files
│?? │?? └── index.html
│?? ├── handlers
│?? │?? └── main.yaml
│?? ├── tasks
│?? │?? └── main.yaml
│?? ├── templates
│?? │?? └── nginx.conf.j2
│?? └── vars
│?? └── main.yaml
└── site.yaml
***准备目录结构***
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
2.编写任务
vim roles/nginx/tasks/main.yaml
---
- name: install epel-release packge
yum: name=epel-release state=latest
- name: install nginx packge
yum: name=nginx state=latest
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: make sure nginx service running
service: name=nginx state=started enabled=yes
3.准备配置文件
vim roles/nginx/templates/nginx.conf.j2
修改:worker_processes {{ ansible_processor_cores }};
worker_connections {{ worker_connections }};
4.编写变量
vim roles/nginx/vars/main.yaml
worker_connections: 10240
5.编写处理程序
vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
6.编写剧本
vim roles/site.yaml
- hosts: host4
roles:
- nginx
7.实施
cd roles
ansible-playbook site.yaml --syntax-check