Ansible教程——wait_for模块


wait_for 模块

wait_for模块在继续执行之前等待一个条件成立.


一、摘要

常用场景:

  • 当服务的初始化脚本返回后不能立即可用时,等待可用状态是很有用的。
  • 当使用community.libvirt.virt模块启动来宾程序并需要暂停直到它们准备好时,它也很有用。
  • 这个模块也可以用在等待匹配字符串出现在文件中后,再继续执行。
  • 这个模块也可以用来等待文件在文件系统上可用或不存在时,再继续执行。

二、参数

参数选项说明
active_connection_statesDefault:
[“ESTABLISHED”,
“FIN_WAIT1”,
“FIN_WAIT2”,
“SYN_RECV”,
“SYN_SENT”,
“TIME_WAIT”]
被算作活动连接的TCP连接状态列表。
connect_timeout默认值:5秒在关闭和重试之前等待连接发生的最大秒数。
delay默认值:0开始轮询之前等待的秒数。
exclude_hosts尝试寻找活跃的TCP连接,耗尽状态时,可以忽略的主机或ip列表。
host默认值:“127.0.0.1”等待可解析的主机名或IP地址。
msg这将覆盖不满足所需条件的正常错误消息。
path在继续之前,文件系统中必须存在的文件的路径。
参数“Path”和“port”互斥,二者只可取其一。
port轮询的端口号。
参数“Path”和“port”互斥,二者只可取其一。
search_regex可用于匹配文件或套接字连接中的字符串。
默认为多行正则表达式。
sleep默认值:1秒在两次检查之间休眠的秒数。
stateChoices:
present
absent
started
stopped
drained
当检查一个端口时,started将确保端口是开放的,stopped将检查状态是关闭的,drained将检查活动连接是耗尽的。
当检查文件或搜索字符串是否存在时,将确保文件或字符串已存在再继续执行,absent将检查该文件是否存在或被删除。
timeout默认值:300秒等待的最大秒数,当与另一个条件一起使用时,它将强制出现错误。
在没有其他条件的情况下,它相当于睡眠

三、属性

属性参数说明
check_modefull是否可以在check_mode下运行,并在修改目标的情况下返回改变状态预测
diff_modenone当在diff模式时,将返回什么已更改的细节(或可能需要在check_mode中更改)
platformPlatform: posix针对可操作的OS

四、示例

# 睡眠300秒,再继续执行play
- name: Sleep for 300 seconds and continue with play
  wait_for:
    timeout: 300
  delegate_to: localhost
 
# 等待8000端口在主机上开放,10秒内不要开始检查
- name: Wait for port 8000 to become open on the host, don't start checking for 10 seconds
  wait_for:
    port: 8000
    delay: 10
   
# 等待任何IP的8000端口关闭活动连接,10秒内不开始检查 
- name: Waits for port 8000 of any IP to close active connections, don't start checking for 10 seconds
  wait_for:
    host: 0.0.0.0
    port: 8000
    delay: 10
    state: drained

# 等待任意IP的8000端口关闭活动连接,忽略指定主机的连接
- name: Wait for port 8000 of any IP to close active connections, ignoring connections for specified hosts
  wait_for:
    host: 0.0.0.0
    port: 8000
    state: drained
    exclude_hosts: 10.2.1.2,10.2.1.3
  
 # 等待直到/tmp/foo文件存在,然后再继续执行 
- name: Wait until the file /tmp/foo is present before continuing
  wait_for:
    path: /tmp/foo

# 等待字符串"completed"出现在/tmp/foo文件中,然后再继续执行
- name: Wait until the string "completed" is in the file /tmp/foo before continuing
  wait_for:
    path: /tmp/foo
    search_regex: completed

# 等待regex模式在/tmp/foo文件中匹配成功,并打印匹配的组
- name: Wait until regex pattern matches in the file /tmp/foo and print the matched group
  wait_for:
    path: /tmp/foo
    search_regex: completed (?P<task>\w+)
  register: waitfor
- debug:
    msg: Completed {{ waitfor['match_groupdict']['task'] }}
 
# 等待锁文件被移除,然后再继续
- name: Wait until the lock file is removed
  wait_for:
    path: /var/lock/file.lock
    state: absent
  
# 等待进程完成并且pid被销毁
- name: Wait until the process is finished and pid was destroyed
  wait_for:
    path: /proc/3466/status
    state: absent

# 失败时输出自定义消息
- name: Output customized message when failed
  wait_for:
    path: /tmp/foo
    state: present
    msg: Timeout to find file /tmp/foo

# 假设inventory_hostname是不可解析的,并且在开始时延迟10秒;等待300秒,端口22被打开并包含“OpenSSH”
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
  wait_for:
    port: 22
    host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
    search_regex: OpenSSH
    delay: 10
  connection: local
 
# 和上面一样,但你通常在inventory中设置ansible_connection,它覆盖了'connection';等待300秒,端口22被打开并包含“OpenSSH”
- name: Wait 300 seconds for port 22 to become open and contain "OpenSSH"
  wait_for:
    port: 22
    host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
    search_regex: OpenSSH
    delay: 10
  vars:
    ansible_connection: local

五、返回值

关键字返回说明
elapsedalways等待时经过的秒数
match_groupdictalways包含匹配的所有命名子组的字典,由子组名称作为键值,如 https://docs.python.org/3/library/re.html#re.MatchObject.groupdict 所返回的那样。
**Sample: **
{‘group’: ‘match’}
match_groupsalways返回的包含匹配的所有子组的元组,如 https://docs.python.org/3/library/re.html#re.MatchObject.groups 所返回的那样。
Sample:
[‘match 1’, ‘match 2’]

总结

根据ansible官网整理所得。
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/wait_for_module.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值