Playbook
1.playbook的组成部分
开头
用 --- 表示,表示是一个yaml文件,但是可以忽略不写
Tasks(任务)
包含了在目标主机上执行的操作,操作还是由模板来执行。每一个任务都是一个ansible的模块,调用系统命令、操作管理等等。
Variables(变量)
存储和传递数据,可以自定义变量,在playbook中变量都是全局的。
Templates模板
用于生成配置文件,可以在ansible当中定义好变量和属性,直接传给配置文件。
Handlers处理器
满足条件时,进行触发的操作,通常用在任务发生"change"时,可以触发这个处理器,一般是用处理器来对服务进行重启
Roles 角色
是一种组织和封装playbooks的方式,类似docker-compose。(现在几乎不用)
2.实例
实例1: 简单的playbook

实例2: vars


实例3: when判断

练习
主机2台,只有当ip=192.168.206.50时,安装nginx,修改访问的页面"this is nginx",修改完之后重启服务。

实例4: 循环
with_items 循环遍历
with_list 列表输出
with_together 同一列表位置数据组合输出的循环
with_nested 列表数据循环匹配

三种循环传参方式



实例5: tags
#tags -->
#debug 在ansible中相当于标签,在执行playbook的时候指定标签来运行我们需要的tasks。
1.debug:调试,调试的结果不会写入到目标主机
2.always:始终会运行的任务。
3.setup:收集主机信息
4.pre_tasks:在主任务之前运行的任务
5.post_tasks:在主任务之后运行的任务
6.自定义标签
7.never:这个任务永远不运行



实例6: template

