Ansible:从JSON响应创建变量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible:从JSON响应创建变量相关的知识,希望对你有一定的参考价值。
我正在努力理解如何从一个uri呼叫中获得响应以传递给另一个。我正在为我的存储系统创建一个剧本。对于在自定义标头“X-HP3PAR-WSAPI-SessionKey”中传递的所有REST调用,它需要一个SessionKey
初始身份验证REST调用成功,我看到了SessionKey。我无法将SessionKey传递给后续调用。
有什么建议?
---
- name: Connect to 3par
hosts: localhost
become: no
vars:
auth_3par_user: "3paruser"
auth_3par_password: "3parpass"
ip_address_3par: 1.1.1.1
rest_api_url_3par: "https://{{ ip_address_3par }}:8080/api/v1"
tasks:
- name: check if 3par WSAPI is running
uri:
url: "{{ rest_api_url_3par }}/credentials"
force_basic_auth: yes
method: POST
HEADER_Content-Type: application/json
body_format: json
body: "{ 'user': '{{ auth_3par_user }}', 'password': '{{ auth_3par_password }}' }"
status_code: 201
return_content: yes
validate_certs: no
register: output
- name: Parsing key
debug:
msg: "{{ output.json.key }}"
- name: GET 3par volumes
uri:
url: "{{ rest_api_url_3par }}/volumes"
method: GET
HEADER_Content-Type: "application/json"
headers:
"X-HP3PAR-WSAPI-SessionKey": "{{ output.json.key }}"
status_code: 200
return_content: yes
validate_certs: no
register: volume_output
- name: Parsing Volumes GET
debug:
var={{ volume_output.stdout | from_json }}
TASK [Parsing key] *************************************************************
task path: /home/chris/ansible/ansible_3par/playbooks/connect_3par.yml:26
ok: [localhost] => {
"msg": "0-f150f34f975693b3c95efa84b13bd605-00cc315a"
}
TASK [check if 3par WSAPI is running] ******************************************
task path: /home/chris/ansible/ansible_3par/playbooks/connect_3par.yml:30
ESTABLISH LOCAL CONNECTION FOR USER: chris
127.0.0.1 EXEC ( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1513213268.76-220322452758647 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1513213268.76-220322452758647 )" )
127.0.0.1 PUT /tmp/tmpgOtWjh TO /home/chris/.ansible/tmp/ansible-tmp-1513213268.76-220322452758647/uri
127.0.0.1 EXEC LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/chris/.ansible/tmp/ansible-tmp-1513213268.76-220322452758647/uri; rm -rf "/home/chris/.ansible/tmp/ansible-tmp-1513213268.76-220322452758647/" > /dev/null 2>&1
fatal: [localhost]: FAILED! => {"changed": false, "connection": "close", "content": "{"code":6,"desc":"invalid session key"}",
Ansible版本2.0.0.2
答案
您正在混合uri
模块中的标头的新旧语法。
当您使用过时的Ansible 2.0版本时,headers
参数将被完全忽略。
升级到当前的Ansible版本。
以供参考:
- 如果您使用Ansible <2.1使用
HEADER_*
定义所有标题, - 如果你使用Ansible> = 2.1定义
headers
下的所有头文件。
另一答案
在修复标题并将Ansible更新到最新版本后发布更正的功能代码以供参考。
---
- name: Connect to 3par
hosts: localhost
become: no
vars:
auth_3par_user: "3paruser"
auth_3par_password: "3parpass"
ip_address_3par: 1.1.1.1
rest_api_url_3par: "https://{{ ip_address_3par }}:8080/api/v1"
tasks:
- name: check if 3par WSAPI is running
uri:
url: "{{ rest_api_url_3par }}/credentials"
method: POST
headers:
Content-Type: "application/json"
body_format: json
body: "{ 'user': '{{ auth_3par_user }}', 'password': '{{ auth_3par_password }}' }"
status_code: 201
return_content: yes
validate_certs: no
register: output
- name: Parsing key
debug:
msg: "{{ output.json.key }}"
- name: GET 3par volumes
uri:
url: "{{ rest_api_url_3par }}/volumes"
method: GET
headers:
Content-Type: "application/json"
X-HP3PAR-WSAPI-SessionKey: "{{ output.json.key }}"
Accept: "application/json"
status_code: 200
return_content: yes
validate_certs: no
register: volume_output
- name: Parsing Volumes GET
debug:
msg: "{{ volume_output }}"enter code here
以上是关于Ansible:从JSON响应创建变量的主要内容,如果未能解决你的问题,请参考以下文章
将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener
编写一个类是从 graphql API 响应返回的 JSON 对象创建变量集合的好方法吗?
PHP:如何从 JSON 中获取变量(从 Swift iOS 应用程序发送)并以 JSON 响应