如何在 Ansible 的 do-until 循环中指定多个条件
How to specify multiple conditions in a do-until loop in Ansible
我正在进行 REST 调用,并希望在继续之前检查我的请求是否已完成。在响应中,我得到了 "PENDING" 或 "IN_PROGRESS" 作为 request_status。我想等到我得到 "COMPLETED" 或 "FAILED"。为此,我想等待 "PENDING" 或 "IN_PROGRESS"
我尝试了很多变体,但都没有成功,最后的尝试如下:
- name: Wait for the cluster deployment (this will take a while)
uri:
url: http://localhost:8080/api/v1/clusters/{{ cluster_name }}/requests/1
method: GET
user: admin
password: admin
HEADER_X-Requested-By: Ansible
force_basic_auth: yes
status_code: 200, 201, 202
return_content: yes
register: response
until: "'{{ (response.content | from_json).Requests.request_status }}' != 'PENDING' AND '{{ (response.content | from_json).Requests.request_status }}' != 'IN_PROGRESS'"
retries: 3
delay: 5
错误是:
"The conditional check ''{{ (response.content
| from_json).Requests.request_status }}' != 'PENDING' AND '{{
(response.content | from_json).Requests.request_status }}' !=
'IN_PROGRESS'' failed. The error was: template error while templating
string: expected token 'end of statement block', got 'AND'. String: {%
if 'COMPLETED' != 'PENDING' AND 'COMPLETED' != 'IN_PROGRESS' %} True
{% else %} False {% endif %}"
所以,我的问题是如何在 Ansible 的 do-until 循环中指定多个条件?
好的,找到问题了。
将 "AND" 更改为 "and" 有效。
until: "'{{ (response.content | from_json).Requests.request_status }}'
!= 'PENDING' and '{{ (response.content |
from_json).Requests.request_status }}' != 'IN_PROGRESS'"
可以(至少使用 Ansible 2.8.3)将数组传递给 until
:
- shell: "echo OK"
until:
- 1 == 1
- "'no'.replace('no', 'yes') == 'yes'"
在这种情况下,数组项隐含地 "joined" 和 and
我正在进行 REST 调用,并希望在继续之前检查我的请求是否已完成。在响应中,我得到了 "PENDING" 或 "IN_PROGRESS" 作为 request_status。我想等到我得到 "COMPLETED" 或 "FAILED"。为此,我想等待 "PENDING" 或 "IN_PROGRESS"
我尝试了很多变体,但都没有成功,最后的尝试如下:
- name: Wait for the cluster deployment (this will take a while)
uri:
url: http://localhost:8080/api/v1/clusters/{{ cluster_name }}/requests/1
method: GET
user: admin
password: admin
HEADER_X-Requested-By: Ansible
force_basic_auth: yes
status_code: 200, 201, 202
return_content: yes
register: response
until: "'{{ (response.content | from_json).Requests.request_status }}' != 'PENDING' AND '{{ (response.content | from_json).Requests.request_status }}' != 'IN_PROGRESS'"
retries: 3
delay: 5
错误是:
"The conditional check ''{{ (response.content | from_json).Requests.request_status }}' != 'PENDING' AND '{{ (response.content | from_json).Requests.request_status }}' != 'IN_PROGRESS'' failed. The error was: template error while templating string: expected token 'end of statement block', got 'AND'. String: {% if 'COMPLETED' != 'PENDING' AND 'COMPLETED' != 'IN_PROGRESS' %} True {% else %} False {% endif %}"
所以,我的问题是如何在 Ansible 的 do-until 循环中指定多个条件?
好的,找到问题了。
将 "AND" 更改为 "and" 有效。
until: "'{{ (response.content | from_json).Requests.request_status }}' != 'PENDING' and '{{ (response.content | from_json).Requests.request_status }}' != 'IN_PROGRESS'"
可以(至少使用 Ansible 2.8.3)将数组传递给 until
:
- shell: "echo OK"
until:
- 1 == 1
- "'no'.replace('no', 'yes') == 'yes'"
在这种情况下,数组项隐含地 "joined" 和 and