Ansible:从JSON响应创建变量

时间:2017-12-14 01:21:38

标签: json ansible

我正在努力理解如何从一个uri呼叫中获得响应以传递给另一个。我正在为我的存储系统创建一个剧本。对于在自定义标头中传递的所有REST调用,它需要一个SessionKey" X-HP3PAR-WSAPI-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

2 个答案:

答案 0 :(得分:0)

您正在混合uri模块中的标头的新旧语法。

当您使用过时的Ansible 2.0版本时,headers参数将被完全忽略。

升级到当前的Ansible版本。

供参考:

  • 如果您使用Ansible< 2.1使用HEADER_*

  • 定义所有标题
  • 如果您使用Ansible> = 2.1定义headers下的所有标题。

答案 1 :(得分:0)

在修复标题并将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