Ansible之Roles
Roles介绍
ansible自1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers。要使用roles只需要在playbook中使用include指令引入即可。简单来讲,roles就是通过分别将变量、文件、任务、模板及处理器放置于单独的目录中,并可以便捷的include他们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。主要使用场景代码复用度较高的情况下。
roles多个角色的集合,可以将多个的role分别放置roles目录下的独立子目录中
ls -l roles/
mysql/
httpd/
nginx/
roles个目录作用
以roles/mysql/:项目为例
目录名称 | 作用 |
---|---|
files/ | 存放copy或script等模块调用的文件 |
templates/ | template模块查找所需要的模板文件的目录 |
tasks/ | 定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其他的文件需要在此文件中通过include进行包含 |
handlers/ | 至少应该包含一个main.yml文件;其他的文件需要在此文件中通过include进行包含 |
vars/ | 定义变量,至少包含一个main.yml文件;其他的文件需要再次文件中通过include进行包含 |
meta/ | 定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其他文件需要在此文件中通过include进行包含 |
default/ | 设定默认变量是使用此目录中的main.yml文件,比vars目录优先级低 |
通过roles创建一个playbook
- 创建相应的目录
cd /etc/ansible/roles/
mkdir httpd/{tasks,files,vars,handlers} -p
- 创建一个安装服务的剧本
vim /etc/ansible/roles/httpd/tasks/install.yml
---
- name: install httpd service
yum: name=httpd
- 创建配置文件剧本
vim /etc/ansible/roles/httpd/tasks/config.yml
---
- name: httpd config
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart-httpd
- 创建httpd主页文件剧本
vim /etc/ansible/roles/httpd/tasks/index.yml
---
- name: httpd index
copy: src=index.html dest=/var/www/html/
- 创建启动服务剧本
vim /etc/ansible/roles/httpd/tasks/service.yml
---
- name: start httpd
service: name=httpd state=started enabled=yes
- 创建task的main.yml文件
vim /etc/ansible/roles/httpd/tasks/main.yml
---
- include: install.yml
- include: config.yml
- include: index.yml
- include: serivce.yml
main.yml文件要注意调用的先后顺序,先执行的在前面
- 创建handlers文件
vim /etc/ansible/roles/httpd/handlers/main.yml
---
- name: restart-httpd
service: name=httpd state=restartd
- 创建httpd配置文件
vim /etc/ansible/roles/httpd/files/httpd.conf
---
ServerRoot "/etc/httpd"
Listen 87
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
- 创建httpd主页文件
vim /etc/ansible/roles/httpd/files/index.html
---
<h1>Hello</h1>
- 创建httpd剧本调用角色
vim /etc/ansible/role_http.yml
---
- hosts: web
remote_user: root
roles:
- httpd
剧本调用的文件要放在与roles目录同级下
- 执行剧本
ansible-playbook role_http.yml