Ansible 重复任务,直到指定的字符串出现在输出中
Ansible repeat task until a specified string is in the output
我有一个任务需要执行,直到标准输出中出现某个字符串。
bigip_command 的标准输出是一个列表 (https://docs.ansible.com/ansible/latest/modules/bigip_command_module.html#return-values)
- name: Disable pool member
bigip_command:
commands: "tmsh show /sys connection ss-server-addr xx.xx.xx.xx ss-server-port 8080"
provider:
user: "xx"
password: "xxx"
server_port: xx
server: xxx
delegate_to: localhost
register: result
until: "'Total records returned: 0' in result.stdout"<br><br>
输出为:
失败 - 重试:禁用池成员(剩余 3 次重试)
但返回的记录总数:0 在 result.stdout。
调试输出为:
- name: debug
debug:
msg: "{{ result.stdout }}"
输出:
ok: [xxx] => {
"msg": [
"Sys::Connections\nTotal records returned: 0"
]
}
参见Retrying a task until a condition is met。尝试
- name: Disable pool member
vars:
my_regex: 'Total records returned: 0'
bigip_command:
commands: "yyy"
user: "xx"
password: "xxx"
server_port: xx
server: xxx
delegate_to: localhost
register: result
until: result.stdout is search(my_regex)
retries: 5
delay: 10
(未测试)
我有一个任务需要执行,直到标准输出中出现某个字符串。 bigip_command 的标准输出是一个列表 (https://docs.ansible.com/ansible/latest/modules/bigip_command_module.html#return-values)
- name: Disable pool member
bigip_command:
commands: "tmsh show /sys connection ss-server-addr xx.xx.xx.xx ss-server-port 8080"
provider:
user: "xx"
password: "xxx"
server_port: xx
server: xxx
delegate_to: localhost
register: result
until: "'Total records returned: 0' in result.stdout"<br><br>
输出为:
失败 - 重试:禁用池成员(剩余 3 次重试)
但返回的记录总数:0 在 result.stdout。
调试输出为:
- name: debug
debug:
msg: "{{ result.stdout }}"
输出:
ok: [xxx] => {
"msg": [
"Sys::Connections\nTotal records returned: 0"
]
}
参见Retrying a task until a condition is met。尝试
- name: Disable pool member
vars:
my_regex: 'Total records returned: 0'
bigip_command:
commands: "yyy"
user: "xx"
password: "xxx"
server_port: xx
server: xxx
delegate_to: localhost
register: result
until: result.stdout is search(my_regex)
retries: 5
delay: 10
(未测试)