# 官方提供@yunTaoScripts PLAYBOOK 🔥🔥
# TASK的执行状态
完整的playbook脚本示例
最基本的playbook脚本分为三个部分:
在什么机器上以什么身份执行
hosts
users
...
执行的任务是都有什么
tasks
善后的任务都有什么
handlers
# 什么是task
- task中每个action会调用一个module,在module中会去检查当前系统状态是否需要重新执行. 具体的判断规则由各个module自己实现.
- 如果执行那么action会得到返回值changed;
- 如果不需要执行,那么action得到返回值ok
# 什么是handler?
- 每个主流的编程语言都会有event机制,那么handler就是playbook的event。 Handlers里面的每一个handler,也是对module的一次调用。而handler与tasks不同的是,handlers不会默认的按顺序执行。 Tasks中的任务都是有状态的,changed或者ok。 Ansible提供了一种机制,只在task的执行状态为changed的时候,才会触发执行,这就是handler。
# 应用场景
什么情况下使用handlers呢?
如果你在tasks中修改了apache的配置文件。需要重起apache。此外还安装了apache的插件。那么还需要重起apache。像这样的应该场景中,重起apache就可以设计成一个handler.
# 一个handler最多只执行一次
- 在所有的任务里表执行之后执行,如果有多个task notify同一个handler,那么只执行一次。
# 定义变量
- 剧本playbook原则
- 如果对这些主机的操作相同,则放到一个playbook。如果不同,则考虑放到两个playbook。
- 先写好框架,再填充内容。
# 手动定义变量
[root@server1 playbook]# cat 1.yaml
---
- name: "定义普通变量"
hosts:
- server2
- server3
vars:
name: "yuntao"
age: 23
tasks:
- name: '打印普通变量'
debug: msg="{{name}}===={{age}}"
[root@server1 playbook]# ansible-playbook 1.yaml
重复定义变量
重复定义变量,后面的变量会覆盖前面的。
# 定义字典变量
[root@server1 playbook]# cat 2.yaml
---
- name: "定义字典变量"
hosts:
- server2
- server3
vars:
user1:
name: "yuntao"
age: 23
user2:
name: "zhangsan"
age: 19
tasks:
- name: '打印字典变量'
debug: msg="{{user1.name}}===={{user2.age}}"
[root@server1 playbook]#
# 定义列表变量
[root@server1 playbook]# cat 3.yaml
---
- name: "定义列表变量"
hosts:
- server2
- server3
vars:
list1: [1,2,3,4]
user:
- name: "yuntao1"
age: 23
- name: "yunta2"
age: 232
- name: "yuntao3"
age: 1
tasks:
- name: '打印列表变量'
#debug: msg="{{user}}===={{user[1]}}===={{user[2].name}}====={{user[0].age}}"
debug: msg="{{list1}}==={{list1[0]}}"
# 引用文件变量
[root@server1 playbook]# cat 4.yaml
---
- name: "定义列表变量"
hosts:
- server2
- server3
vars_files:
- file1
tasks:
- name: '打印列表变量'
#debug: msg="{{user}}===={{user[1]}}===={{user[2].name}}====={{user[0].age}}"
debug: msg="{{list1}}==={{list1[0]}}"
[root@server1 playbook]# cat file1
list1: [1,2,3,4]
user:
- name: "yuntao1"
age: 23
- name: "yunta2"
age: 232
- name: "yuntao3"
age: 1
[root@serve
# 注册变量 register
- shell 模块 执行命令 结果不显示在控制台,需要变量保存。
[root@server1 playbook]# cat 5.yaml
---
- name: "定义注册变量"
hosts:
- server2
- server3
tasks:
- name: '注册变量'
register: xx
shell: cat /etc/hosts
- name: '打印注册变量'
debug: msg="{{xx.stdout_lines}}"
- rc 代表return code,正确执行返回 0
[root@server1 playbook]# cat 5.yaml
---
- name: "定义注册变量"
hosts:
- server2
- server3
tasks:
- name: '注册变量'
register: xx
shell: cat /etc/hostsxx
- name: '打印注册变量'
debug: msg="{{xx}}"
when: xx.rc == 1 # 由于返回值为 0, 所以该task没有执行,不会打印信息。
[root@server1 playbook]# cat 5.yaml
---
- name: "定义注册变量"
hosts:
- server2
- server3
tasks:
- name: '注册变量'
register: xx
ignore_errors: yes
shell: cat /etc/hostsxx
- name: '打印注册变量'
debug: msg="{{xx}}"
when: xx.rc == 1 # 由于上一步报错,返回值为1,会打印信息。
ignore_errors=yes
在执行playbook的时候报错,后面的playbook都不再执行。可增加ignore_errors=yes
忽略报错。
# gather_facts变量
- playbook 默认会执行
gather_facts
任务,也就是使用setup
模块获取系统参数 - 可通过
gather_facts: false
屏蔽该任务,默认true
[root@server1 playbook]# cat 6.yaml
---
- name: "gather_facts变量"
hosts:
- server2
- server3
#gather_facts: false
tasks:
- name: '打印IP '
debug: msg="{{ansible_default_ipv4.address}}"
- name: '打印主机名'
debug: msg="{{ansible_fqdn}}"
- name: '打印bios版本'
debug: msg="{{ansible_bios_version}}"
- name: '打印内存大小'
debug: msg="{{ansible_memtotal_mb}}"
- name: '打印/dev/sda大小'
debug: msg="{{ansible_devices.sda.size}}"
- name: '打印/dev/sda1大小 '
debug: msg="{{ansible_devices.sda.partitions.sda1.size}}"
- name: '打印系统版本'
debug: msg="{{ansible_distribution_version}}"
# 内置变量
Parameter | Description |
---|---|
hostvars | A dict whose keys are Ansible host names and values are dicts that map variable names to values |
group_names | A list of all groups that the current host is a member of |
groups | A dict whose keys are Ansible group names and values are list of hostnames that are members of the group. Includes all and ungrouped groups: {"all": [...], "web": [...], "ungrouped": [...]} |
inventory_hostname | Name of the current host as known by ansible. |
play_hosts | A Ilist of inventory hostnames that are active in the current play (or current batch if running serial) |
ansible_version | A dict with ansible version info: {"full": 1.8.1", "major": 1, "minor": 8,"revision": 1, "string": "1.8.1"} |
role_path | The current role's pathname (available only inside a role) |
# groups
- 打印所有主机组
[root@server1 playbook]# cat 7.yaml
---
- name: "内置变量"
hosts:
- server2
- server3
tasks:
- name: '打印所有主机组变量'
debug: msg="{{groups}}"
- name: '打印xyt主机组变量'
debug: msg="{{groups.xyt}}"
# group_names
- 打印主机所在主机组名称
[root@server1 playbook]# cat 8.yaml
---
- name: "内置变量"
hosts:
- server2
- server3
tasks:
- name: '打印所在主机组名称'
debug: msg="{{group_names}}"
# hostvars
- 打印其他主机组变量
---
- name: "内置变量"
hosts:
- server2
tasks:
- name: '打印其他主机组变量'
debug: msg="{{hostvars['server3'].ansible_fqdn}}"
获取server3主机名报错
TASK [打印其他主机组变量] *********************************************************************************************************************
fatal: [server2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'\n\nThe error appears to be in '/root/ansible/playbook/9.yaml': line 6, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: '打印其他主机组变量'\n ^ here\n"}
- 解决办法,增加playbook。
[root@server1 playbook]# cat 9.yaml
---
- hosts:
- server3
- name: "内置变量"
hosts:
- server2
tasks:
- name: '打印其他主机组变量'
debug: msg="{{hostvars['server3'].ansible_fqdn}}"
# inventory_hostname
- 显示每台主机在清单文件中名字
[root@server1 playbook]# cat 10.yaml
---
- name: "内置变量"
hosts:
- server2
- server3
tasks:
- name: '打印主机在清单文件的名字'
debug: msg="{{inventory_hostname}}"
# 变量过滤器
[root@server1 playbook]# cat 11.yaml
---
- name: "变量过滤器"
hosts:
- server3
vars:
aa: 5
bb: '6'
cc: [1,2,3,4,5,6,1,0,-1]
dd: [1,2,3,4,5,6]
tasks:
- name: '数学运算'
debug: msg="{{3+7}}==={{(-3)+7}}"
- name: '绝对值'
debug: msg="{{(-3|abs)+7}}"
- name: '浮点'
debug: msg="{{(3|float)+7}}==={{(-3|float)+7}}"
- name: '四舍五入'
debug: msg="{{(3.5|round)+7}}==={{(-3.5|round)+7}}"
- name: '变量引用'
debug: msg="{{aa+7}}"
- name: '类型转换'
debug: msg="{{(bb|int)+5}}==={{(aa|string)}}"
- name: '大小写转换'
debug: msg="{{'yuntao'|upper}}==={{('YUNTAO'|lower)}}"
- name: '列表操作'
debug: msg="{{cc|length}}==={{(cc|max)}}==={{cc|min}}"
- name: '列表操作'
debug: msg="{{dd|shuffle}}==={{cc|sum}}==={{cc|sort}}"
- name: '默认值'
debug: msg="{{aa|default('NULL')}}==={{ff|default('NULL')}}" #不存在就使用默认值
- name: '加密'
debug: msg="{{'ahfdoi'|hash('md5')}}==={{aa|hash('sha1')}}==={{bb|hash('sha512')}}"
- name: '用户密码加密'
debug: msg="{{bb|password_hash('sha512')}}"
← 快速链接