我正在使用Ansible,遇到了一个我不理解或不知道如何解决的问题。希望有人能帮助我
问题发生在failed_when
条件中:
fatal: [localhost]: FAILED! => {
"msg": "The conditional check ''must not contain special characters or whitespace' not in result.results[0].json['localizedMessage']' failed. The error was: error while evaluating conditional ('must not contain special characters or whitespace' not in result.results[0].json['localizedMessage']): 'dict object' has no attribute 'results'"
}
我的任务是:
- name: Create instance - must not contain special characters or whitespace
uri:
url: "https://{{PROXY_ADDRESS}}/api/sol005/etsi/nslcm/v2/ns_instances"
status_code: 400
validate_certs: no
body_format: json
method: POST
headers:
Content-type: application/json
Authorization: "{{'Bearer'+' '+access_token}}"
Version: "2.0.0"
Accept: "*/*"
body: >
{ "nsdId": "instance::int::1.0",
"nsName": "{{ item }}",
"nsDescription": "Test instance with name containing invalid characters."
}
register: result
# - set_fact:
# localizedMessage: "{{ result.results[0].json['localizedMessage'] }}"
with_items:
- "caesar:test"
failed_when: "'must not contain special characters or whitespace' not in result.results[0].json['localizedMessage']"
在尝试调试时,我编写了以下内容:
- debug:
msg:
- "============ Printing result output ============"
- "Showing result type: {{ result | type_debug }}"
- "Showing result contents: {{ result}}"
- "Showing result.keys(): {{ item }}"
with_items: "{{ result.keys() }}"
- debug:
msg:
- "============ Printing result.results output ============"
- "Showing result.results type: {{ result.results | type_debug }}"
- "Showing result.results contents: {{ result.results }}"
- debug:
msg:
- "============ Printing result.results[0] output ============"
- "Showing result.results[0].type: {{ result.results[0] | type_debug }}"
- "Showing result.results[0].contents: {{ result.results[0] }}"
- "Showing result.results[0].keys(): {{ item }}"
with_items: "{{ result.results[0].keys() }}"
- debug:
msg:
- "============ Printing result.results[0].json output ============"
- "Showing result.results[0].json type: {{ result.results[0].json | type_debug }}"
- "Showing result.results[0].json contents: {{ result.results[0].json }}"
- "Showing result.results[0].json.keys(): {{ item }}"
with_items: "{{ result.results[0].json.keys() }}"
- debug:
msg:
- "============ Printing result.results[0].json.localizedMessage output ============"
- "Showing result.results[0].json.localizedMessage type: {{ result.results[0].json.localizedMessage | type_debug }}"
- "Showing result.results[0].json.localizedMessage contents: {{ result.results[0].json.localizedMessage }}"
我认为,这里的重要信息是:
"============ Printing result output ============",
"Showing result type: dict",
"Showing result.keys(): dict_keys(['results', 'msg', 'changed'])"
"============ Printing result.results output ============",
"Showing result.results type: list",
"============ Printing result.results[0] output ============",
"Showing result.results[0].type: dict",
"Showing result.results[0].keys(): dict_keys(['redirected', 'url', 'status', 'x_content_type_options', 'x_xss_protection', 'cache_control', 'pragma', 'expires', 'strict_transport_security', 'x_frame_options', 'vary', 'content_type', 'transfer_encoding', 'date', 'connection', 'server', 'msg', 'elapsed', 'changed', 'json', 'invocation', 'failed', 'item', 'ansible_loop_var'])"
"============ Printing result.results[0].json output ============",
"Showing result.results[0].json type: dict",
"Showing result.results[0].json.keys(): dict_keys(['url', 'localizedMessage', 'details'])"
"============ Printing result.results[0].json.localizedMessage output ============",
"Showing result.results[0].json.localizedMessage type: AnsibleUnsafeText",
"Showing result.results[0].json.localizedMessage contents: A INVALID_REQUEST error has occurred: name must not contain special characters or whitespace (excluding: '-', '_')"
我不熟悉Ansible,也许这就是我没有看到它的原因,但是用'dict object' has no attribute 'results'
这个词表示result
,这是一个dict,没有我调试时建议的results
属性。
我已经能够穿越到result.results[0].json
&result.results[0].json.localizedMessage
所以我觉得还可以
有人能提供建议吗?我想了解我哪里出了问题,或者我会采取其他方法执行failed_when
检查的建议
您的问题来自Ansible以一种非常独特的方式创建
results
:results
键,然后根据所有结果填充该键李>这在这句话和例子中用非常简短的方式描述:
来源:https://docs.ansible.com/ansible/2.9/user_guide/playbooks_loops.html#registering-variables-with-a-loop
因此,如果您想要一个自注册的任务来检查其自身的项目状态,您只需执行以下操作:
以下是两个示例剧本:
相关问题 更多 >
编程相关推荐