我们在使用playbook编写任务时,如果一个httpd需要重复安装的话,我们就需要在playbook中重新写一份儿yml。而role可以将tasks,template等分开定义。将任务,模板存放在单独的目录中。可以将多个规则调用在一起,实现完成复杂场景的实现
role存放规则
每个角色都有自己的相对应的目录结构进行组织。
分步示例
以nginx为例,实现ansible主机提供nginx反代功能,后端两个nginx服务器分别提供静态资源和动态资源的访问
1)定义主机清单
[root@nginx ~]# cat /etc/ansible/hosts
[local_nginx]
11.2.3.25
[http_server]
11.2.3.63
[php_server]
11.2.2.228
2)在/etc/ansible/roles下创建nginx文件夹,并创建tasks,template,var等目录
[root@nginx ansible]# tree
.
├── ansible.cfg
├── hosts
├── nginx_roles.yaml
└── roles
└── nginx
├── default
├── files
│ ├── index.html
│ └── index.php
├── handlers
│ └── main.yaml
├── meta
├── tasks
│ └── main.yaml
├── templates
│ ├── php_server.conf.j2
│ ├── proxy_server.conf.j2
│ ├── server.conf.j2
│ └── www.conf.j2
└── vars
└── main.yaml
3)各个文件的示例,
[root@nginx ansible]# cat roles/nginx/tasks/main.yaml
- name: install epel-release
yum: name=epel-release.noarch state=present
- name: install nginx
yum: name=nginx state=present
- name: start nginx
service: name=nginx state=started
- name: install php-fpm
yum: name=php-fpm state=present
when: ansible_hostname == 'php_nginx'
- name: start php-fpm
service: name=php-fpm state=started
when: ansible_hostname == 'php-fpm'
- name: install php-fpm_conf
template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
notify: restart php-fpm
when: ansible_hostname == 'php_nginx'
- name: install http_server_conf
template: src=server.conf.j2 dest=/etc/nginx/conf.d/server.conf
notify: reload nginx
when: ansible_hostname == 'http_nginx'
- name: install php_server_conf
template: src=php_server.conf.j2 dest=/etc/nginx/conf.d/server.conf
notify: reload nginx
when: ansible_hostname == 'php_nginx'
- name: install proxy_nginx_conf
template: src=proxy_server.conf.j2 dest=/etc/nginx/conf.d/server.conf
notify: reload nginx
when: ansible_hostname == 'nginx'
- name: make directory
file: name={
{
index_root }} state=directory
- name: cp file
copy: src=index.html dest={
{
index_root }}
when: ansible_hostname == 'http_nginx'
- name: cp php_file
copy: src=index.php dest={
{
index_root }}
when: ansible_hostname == 'php_nginx'
[root@nginx nginx]# cat templates/proxy_server.conf.j2 #这是nginx作为反代的配置文件
server{
listen 80;
server_name {
{ domain_name }};
root {
{ index_root }};
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma){
index index.html;
proxy_pass http://{
{ http_server }};
}
location ~ .*\.php$ {
index index.php;
proxy_pass http://{
{ php_server }};
}
}
[root@nginx nginx]# cat templates/server.conf.j2 # 这是处理静态的nginx主机配置文件
server{
listen 80 default_server; #此处加了default_server,所以在tasks中需要也添加一个nginx.conf.j2的文件,将nginx.conf中的default_server去掉,避免影响冲突。
server_name {
{ domain_name }};
location /{
index index.html;
root {
{ index_root }};
}
}
[root@nginx nginx]# cat templates/php_server.conf.j2 #处理动态资源的nginx主机配置文件
server{
listen 80 default_server;
server_name {
{ domain_name }};
index index.php;
root {
{ index_root }};
location / {
root {
{ index_root }};
index index.php;
fastcgi_pass {
{ php_server }}:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME {
{ index_root }}$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx nginx]# cat templates/www.conf.j2 #这个是php-fpm的配置文件,可以根据自己修改
[root@nginx nginx]# cat vars/main.yaml #这个是变量设置文件
index_root: /data/root/
http_server: 11.2.3.63
php_server: 11.2.2.228
domain_name: www.ydong.com
[root@nginx nginx]# ls files/ #这个是后端nginx的网页
index.html index.php
直接运行后的结果