wait_for
模块在继续执行之前等待一个条件成立.
常用场景:
参数 | 选项 | 说明 |
---|---|---|
active_connection_states | Default: [“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秒 | 在两次检查之间休眠的秒数。 |
state | Choices: present absent started stopped drained | 当检查一个端口时,started 将确保端口是开放的,stopped 将检查状态是关闭的,drained 将检查活动连接是耗尽的。当检查文件或搜索字符串是否存在时,将确保文件或字符串已存在再继续执行, absent 将检查该文件是否存在或被删除。 |
timeout | 默认值:300秒 | 等待的最大秒数,当与另一个条件一起使用时,它将强制出现错误。 在没有其他条件的情况下,它相当于睡眠 |
属性 | 参数 | 说明 |
---|---|---|
check_mode | full | 是否可以在check_mode下运行,并在修改目标的情况下返回改变状态预测 |
diff_mode | none | 当在diff模式时,将返回什么已更改的细节(或可能需要在check_mode中更改) |
platform | Platform: 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
关键字 | 返回 | 说明 |
---|---|---|
elapsed | always | 等待时经过的秒数 |
match_groupdict | always | 包含匹配的所有命名子组的字典,由子组名称作为键值,如 https://docs.python.org/3/library/re.html#re.MatchObject.groupdict 所返回的那样。 **Sample: ** {‘group’: ‘match’} |
match_groups | always | 返回的包含匹配的所有子组的元组,如 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