Ansilbe自1.2版本引入的新特性,用于层次性、结构化地组织Playbook,Roles能够根据层次型结构自动装载变量文件、Tasks以及Handlers等使用Roles只需要在Playbook中使用include指令即可。简单来讲,Roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。
/etc/ansible/roles/xxx/ | 具体的角色项目名称 |
---|---|
defaults | 用于为当前角色设定默认变量,此目录应当包含一个main.yml文件 |
files | 用来存放由copy模块或script模块等模块调用的文件 |
handlers | 用于定义此角色中触发条件时执行的动作,此目录应当包含一个main.yml文件 |
meta | 用于定义此角色的特殊设定及其依赖关系,此目录应当包含一个main.yml文件 |
tasks | 用于定义当前角色的任务列表,此目录应当包含一个main.yml文件 |
templates | 用来存放jinjia2模板,template模块会自动在此目录中寻找jinjia2模板文件 |
tests | 用于存放测试role本身功能的Playbook和主机定义文件,在开发测试阶段比较常用,此目录应当包含一个main.yml文件和自身资源设定invetory |
vars | 用于定义此角色用到的变量,此目录应当包含一个main.yml文件 |
mkdir -p /etc/ansible/roles/nginx/tasks/
mkdir -p /etc/ansible/roles/nginx/templates
mkdir -p /etc/ansible/roles/nginx/vars
mkdir -p /etc/ansible/roles/nginx/handlers
cp /etc/nginx/nginx.conf /etc/ansible/roles/nginx/templates/nginx.conf.j2
修改/etc/ansible/roles/nginx/templates/nginx.conf.j2,添加变量
worker_processes {{ ansible_processor_vcpus*2 }};
......
listen {{ http_port }} default_server;
listen [::]:{{ http_port }} default_server;
cat > /etc/ansible/roles/nginx/vars/main.yml << EOF
http_port: 8080
EOF
cat > /etc/ansible/roles/nginx/tasks/install_nginx.yml << EOF
- name: install nginx
yum: name=nginx
EOF
cat > /etc/ansible/roles/nginx/tasks/copy_conf.yml << EOF
- name: copy conf
template: src=templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart service
EOF
cat > /etc/ansible/roles/nginx/tasks/start_service.yml << EOF
- name: start service
service: name=nginx state=started enabled=yes
EOF
cat > /etc/ansible/roles/nginx/tasks/stop_firewalld.yml << EOF
- name: stop firewalld
command: systemctl stop firewalld
EOF
cat > /etc/ansible/roles/nginx/tasks/main.yml << EOF
- include: install_nginx.yml
- include: copy_conf.yml
- include: start_service.yml
- include: stop_firewalld.yml
EOF
cat > /etc/ansible/roles/nginx/handlers/restart_service.yml << EOF
- name: restart service
service: name=nginx state=restarted
EOF
cat > /etc/ansible/roles/nginx/handlers/main.yml << EOF
- include: restart_service.yml
EOF
通过如下几种方式传递参数和条件:
1、在变量文件指定变量参数
cat > /etc/ansible/roles/nginx_role1.yml << EOF
- hosts: all
remote_user: root
roles:
- role: nginx
EOF
执行nginx_role1.yml
ansible-playbook /etc/ansible/roles/nginx_role1.yml
2、在Roles中指定变量
cat > /etc/ansible/roles/nginx_role2.yml << EOF
- hosts: all
remote_user: root
roles:
- { role: nginx,http_port: 8080 }
EOF
执行nginx_role2.yml
ansible-playbook /etc/ansible/roles/nginx_role2.yml
3、when条件判断
cat > /etc/ansible/roles/nginx_role3.yml << EOF
- hosts: all
remote_user: root
roles:
- { role: nginx,http_port: 8080, when ansible_distribution_major_version == '7' }
EOF
执行nginx_role3.yml
ansible-playbook /etc/ansible/roles/nginx_role3.yml
4、tags添加标签
cat > /etc/ansible/roles/nginx_role4.yml << EOF
- hosts: all
remote_user: root
roles:
- { role: nginx,http_port: 8080, tags: nginx }
EOF
执行nginx_role4.yml
ansible-playbook -t nginx /etc/ansible/roles/nginx_role4.yml