playbook的使用
1、介绍
playbook(剧本):是ansible用于配置,部署和管理托管主机剧本,通过playbook的详细描述,执行其中的一系列tasks,可以让远端主机达到预期状态
2、playbook语法格式
2.1语法格式
- playbook由YAML语言编写,遵循YAML标准
- 在同一行中,#之后的内容表示注释
- 同一个列表中的元素应该保持相同的缩进
- playbook由一个或多个play组成
2.2 playbook构成及格式
- hosts:定义将要执行playbook的远程主机组
- vars:定义playbook运行时需要使用的变量
- tasks:定义将要在远程主机上执行的任务列表
- handlers:定义task执行完成以后需要调用的任务
---
- name: 安装http服务,并设置开机自启 //任务的名称
hosts: httpd //执行任务的主机。前面两个空格
tasks: //具体任务。前面两个空格
- name: httpd //小任务名称。前面4个空格
yum: //使用的模块。前面4个空格
name: httpd //模块的名称。6个空格
state: latest //状态:最新。6个空格
~
3、运行playbook
3.1运行的方式
- 直接运行
[root@localhost yum.repos.d]# ansible-playbook wjm.yml
PLAY [安装http服务,并设置开机自启] **********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
ok: [192.168.252.129]
TASK [httpd] *********************************************************************************************************************************
changed: [192.168.252.129]
TASK [service is enabled] ********************************************************************************************************************
changed: [192.168.252.129]
PLAY RECAP ***********************************************************************************************************************************
192.168.252.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 空运行,先让让它空载运行,不实际运行。一般用于测试,在不知道文件配置是否偶问题的时候推荐使用该命令。
用ansible-playbook -C [文件名]
命名来执行空运行
[root@localhost yum.repos.d]# ansible-playbook -C wjm.yml
PLAY [安装http服务,并设置开机自启] **********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************
ok: [192.168.252.129]
TASK [httpd] *********************************************************************************************************************************
changed: [192.168.252.129]
TASK [service is enabled] ********************************************************************************************************************
changed: [192.168.252.129]
PLAY RECAP ***********************************************************************************************************************************
192.168.252.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.2 语法验证
在执行playbook之前,最好要进行验证,确保其内容的语法正确无误。ansible-playbook命令提供了一个–syntax-check选项,可用于验证playbook的语法。ansible-playbook --syntax-check [文件名]
语法正确:
[root@localhost yum.repos.d]# ansible-playbook --syntax-check wjm.yml
playbook: wjm.yml
语法报错:
[root@localhost yum.repos.d]# ansible-playbook --syntax-check wjm.yml
ERROR! the field 'hosts' is required but was not set
//错误! 字段“hosts”是必需的,但未设置
4、实时多个palybook
Playbook是一个YAML文件,含有由一个或多个play组成的列表。记住一个play按顺序列出了要对清单中的选定主机执行的任务。因此,如果一个playbook中有多个play,每个play可以将其任务应用到单独的一组主机。
---
- name: 安装http服务,并设置开机自启
hosts: httpd
tasks:
- name: httpd
yum:
name: httpd
state: latest
- name: 安装php服务,并设置开机自启
hosts: PHP
tasks:
- name: PHP
yum:
name: PHP
state: present
5、查找用于任务的模块
在日常使用中,如果忘记了模块的使用完整名称或者使用ansible-doc -l
命令使用帮助文档,在帮助文档中查找相应的模块,如果知道名字但忘记了用法可以使用ansible-doc [模块名称]
来查看模块的使用方法。最好是能记住名字中的关键字不然很难找到,因为模块一共有3387个之多。
或者在官网查找:https://docs.ansible.com/ansible-core/devel/collections/index_module.html
[root@localhost ~]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices'...
a10_server_axapi3 Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
a10_service_group Manage A10 Networks AX/SoftAX/Thunder/vThunder devices'...
a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices'...
......
......
[root@localhost ~]# ansible-doc user //查看user用户模块的帮助文档
> USER (/usr/lib/python3.6/site-packages/ansible/modules/system/user.py)
Manage user accounts and user attributes. For Windows targets, use the [win_user] module
instead.
* This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):
- append
If `yes', add the user to the groups specified in `groups'.
If `no', user will only be added to the groups specified in `groups', removing them from
all other groups.
Mutually exclusive with `local'
[Default: False]
type: bool
[root@localhost ~]# ansible-doc -l | wc -l //wc:统计 -l:行数
3387 //一共有3387个模块。
6、playbook的语法变化
6.1 YAML注释
注释也可以用于提高可读性。在YAML中,编号或井号字符(#)右侧的所有内容都是注释。如果注释的左侧有内容,请在该编号符号的前面加一个空格。
# This is a YAML comment
some data # This is also a YAML comment
注释的特点:
- 执行期间会跳过注释的块
- 注释有助于添加指定代码块的描述
6.2 YAML字符串
YAML中的字符串最好是使用引号 (单引号、双引号) 引起来,不然在一些特定环境中,命令执行的结果可能会有偏差。
- name: 添加PHP类型
lineinfile:
path: /etc/httpd/conf/httpd.conf
insertafter: ' AddType application/x-gzip .gz .tgz'
line: ' AddType application/x-httpd-php .php '
如果要编写很长的内容的时候可以使用 “ > ” 符号。来将输出的结果写在一行中,这种方式适用于要写很长的配置的时候可以使用这种方法,方便查看且美观
fold_newlines: >
This is an example
of a long string,
that will become
a single sentence once folded.
6.3 过时的playbook简写方法
过时的用法 不推荐使用这种方式。但是这种方式现在还是可以正常运行的
tasks:
- name: shorthand form
service: name=httpd enabled=true state=started
普通形式 普通形式的行数较多,但更容易操作。任务的关键字垂直堆叠,更容易区分。阅读play时,眼睛直接向一扫视,左右运动较少。而且,普通语法是原生的YAML。
tasks:
- name: normal form
service:
name: httpd
enabled: true
state: started