1.YAML介绍
YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy dt Net与Oren Ben-Kiki也是这语言的共同设计者。
YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性:
YAML的可读性好
YAML和脚本语言的交互性好
YAML使用实现语言的数据类型
YAML有一个一致的信息模型
YAML易于实现
YAML可以基于流来处理
YAML表达能力强,扩展性好
更多的内容及规范参见http://www.yaml.org。
YAML语法
YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例。
1 2 3 4 5 6 7 |
|
children:
1 2 3 4 5 6 |
|
YAML文件扩展名通常为.yaml,如example.yaml。
2.Ansible基础元素
通过roles传递变量
当给一个主机应用角色的时候可以传递变量,然后在角色内使用这些变量,示例如下:
1 2 3 4 |
|
通过roles传递变量
当给一个主机应用角色的时候可以传递变量,然后在角色内使用这些变量,示例如下:
1 2 3 4 |
|
Inventory
ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名。默认的inventory file为/etc/ansible/hosts。
inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成。
inventory文件格式
inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。
1 |
|
1 2 3 |
|
1 2 3 4 |
|
如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:
1 2 3 4 |
|
3.ansible playbooks
playbook的组成结构:
Inventory
Modules
Ad Hoc Commands
Playbooks
Tasks:任务,即调用模块完成的某操作
Variables:变量
Templates:模板
Handlers:处理器,由某事件触发执行的操作
Roles:角色
基本结构:
- host: websrvs
remote_user:
tasks:
- task1
module_name: module_args
- task 2
实例1:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
实例2:
1 2 3 4 5 6 7 8 9 |
|
handlers
用于当关注的资源发生变化时采取一定的操作。
“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handler是task列表,这些task与前述的task并没有本质上的不同。
1 2 3 4 5 |
|
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
脚本中定义变量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
when
简单示例:
1 2 3 4 5 6 7 8 |
|
在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法。例如:
1 2 3 4 |
|
when语句中还可以使用Jinja2的大多“filter”,例如要忽略此前某语句的错误并基于其结果(failed或者sucess)运行后面指定的语句,可使用类似如下形式:
1 2 3 4 5 6 7 8 9 10 |
|
此外,when语句中还可以使用facts或playbook中定义的变量。
迭代
当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句来指明迭代的元素列表即可。例如:
1 2 3 4 5 |
|
上面语句的功能等同于下面的语句:
1 2 3 4 |
|
迭代:重复同类task时使用
调用:item
定义循环列表:with_items
- apache
- php
- mysql-server
注意:with_items中的列表值也可以是字典, 但引用时要使用item.KEY
1 2 3 |
|
tags:
在playbook可以为某个或某些任务定义一个“标签”,在执行此playbook时,通过为ansible-playbook命令使用--tags选项能实现仅运行指定的tasks而非所有的;
1 2 3 4 |
|
特殊tags: always
roles
ansilbe自1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。
一个roles的案例如下所示:
site.yml
webservers.yml
fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
meta/
而在playbook中,可以这样使用roles:
---
- hosts: webservers
roles:
- common
- webservers
也可以向roles传递参数,例如:
---
1 2 3 4 5 |
|
甚至也可以条件式地使用roles,例如:
---
- hosts: webservers
roles:
- { role: some_role, when: "ansible_os_family == 'RedHat'" }
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|