* Ansible roles 是为了层次化,结构化的组织Playbook
* roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们
* roles一般用于基于主机构建服务的场景中,在企业复杂业务场景中应用的频率很高
* 以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files等;相当于函数的调用把各个功能切割成片段来执行。
files | 存放copy或script等模块调用的函数 |
---|---|
tasks | 定义各种task,要有main.yml,其他文件include包含调用 |
handlers | 定义各种handlers,要有main.yml,其他文件include包含调用 |
vars | 定义variables,要有main.yml,其他文件include包含调用 |
templates | 存储由template模块调用的模板文本 |
meta | 定义当前角色的特殊设定及其依赖关系,要有main.yml的文件 defaults |
tests | 用于测试角色 |
roles_path = path/roles (默认目录:/etc/ansible/roles)
vim ansible.cfg
roles_path = ~/
ansible-galaxy init apache
ansible-galaxy list
1playbook中使用roles
---
- hosts: server2
roles:
- role: role1
- role: role2
var1: value1 ##此处变量会覆盖roles中的定义变量
2控制任务执行顺序
---
- hosts: server2
roles:
- role: role1 ##角色任务
pre_tasks: ##角色执行前执行的play
- tasks1
tasks: ##普通任务
- tasks2
post_tasks: ##在角色和普通任务执行完毕后执行的play
- tasks3
handlers:
示例
角色:将完整的playbook拆分开
default www.westos.com page
www.westos.com /var/www/www.westos.com/html index:www.westos.com
linux.westos.com / var/www/linux.westos.com/html index:linux.westos.com
vim ansible.cfg
roles_path = ~/
1 生成一个角色
ansible-galaxy init apache
2 在生成的角色目录下
[westos@ansible ~]$ cat apache/vars/main.yml
###用来写变量
---
vhost:
- root: /var/www/html
- root: /var/www/virtual/westos.com/www/html
name: www.westos.com
- root: /var/www/virtual/westos.com/linux/html
name: linux.westos.com
[westos@ansible ~]$ cat apache/tasks/main.yml
##写任务
---
- name: template
block:
- name: install apache
dnf:
name: httpd
state: latest
notify: firewalld
- name: configure apche file
template:
src: vhost.j2
dest: /etc/httpd/conf.d/vhost.conf
notify: restart apache
- name: mkdir Document
file:
path: "{
{item}}"
state: directory
loop::
- /var/www/www.westos.com/html
- /var/www/linux.westos.com/html
- name: create index.html
copy:
dest: "{
{ item.root}}/index.html"
content: "{
{ item.index }}"
loop:
- root: /var/www/html
index: default
- root: /var/www/www.westos.com/html
index: www.westos.com
- root: /var/www/linux.westos.com/html
index: linux.westos.com
rescue:
- debug:
msg: dnf repo is not created
[westos@ansible ~]$ cat apache/templates/vhost.j2
##模板
{
% for webserver in vhost %}
{
% if webserver.name is not defined %}
<VirtualHost _defult_:80>
{
% endif %}
{
% if webserver.name is defined %}
<VirtualHost *:80>
{
% endif %}
{
% if webserver.name is defined %}
ServerName {
{
webserver.name }}
{
% endif %}
DocumentRoot {
{
webserver.root}}
{
% if webserver.name is not defined %}
CustomLog logs/default.log combined
{
% endif %}
{
% if webserver.name is defined %}
CustomLog logs/{
{
webserver.name }}.log combined
{
% endif %}
</VirtualHost>
{
% endfor %}
[westos@ansible ~]$ cat apache/handlers/main.yml
###触发器
---
- name: restart apache
service:
name: httpd
state: restarted
enabled: yes
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
[westos@ansible ~]$ cat vhostest.yml
##测试playbook
---
- name: test roles
hosts: 172.25.11.1
roles:
- role: apache
ansible-playbook vhostest.yml
* Ansible Galaxy 是一个免费共享和下载 Ansible 角色的网站,可以帮助我们更好的定义和学习roles。
* ansible-galaxy命令默认与https://galaxy.ansible.com网站API通信,可以查找、下载各种社区开发的 Ansible 角色
* ansible-galaxy在 Ansible 1.4.2 就已经被包含了
* 在galaxy.ansible.com网站查询roles
安装选择的角色
1网络上
主机需要联网
ansible-galaxy install geerlingguy.nginx
2
vim install_apache_role.yml
---
- src: file:///mnt/apache.tar.gz
name: apache
[westos@ansible ~]$ sudo mv apache /westos/
[westos@ansible ~]$ ls
ansible.cfg vhost.yml
apache.install.yml install_apache_role.yml
inventory
tar zcf apache.tar.gz apache/
[westos@ansible ~]$ ansible-galaxy install -r install_apache_role.yml
##指定路径
- downloading role from file:///mnt/apache.tar.gz
- extracting apache to /home/westos/apache
- apache was installed successfully
[westos@ansible ~]$ ls
ansible.cfg apache
apache.install.yml install_apache_role.yml
inventory