簡體   English   中英

如何將Ansible Play中的大型JSON結果傳遞到python腳本中

[英]How to pass large json result from ansible play into python script

從Ansible劇本中將非常大的json結果傳遞到python腳本時,出現“參數列表過長”錯誤。

基本手冊是:

- name: "The Playbook"
  hosts: "localhost"

  tasks:
    - name: "Get JSON from API"
      uri:
        url: "https://my.real.api.url.goes.here.com"
        method: GET
        return_content: yes
        headers:
          Content-Type: "application/json"
          Authorization: "my api token goes here and works."
      register: result

    - name: Run Py script
      command: python get_tenants.py {{ result }}
      become: yes
      become_user: root
      register: pyout

我也嘗試將結果傳遞為:

{{ result | map(attribute='content') }}

它會將指針傳遞給地圖,但不會將地圖本身作為單個項目傳遞。

我也嘗試將結果傳遞為:

{{ result | map(attribute='content') | list }}

無論如何,我得到:

"failed": true, "msg": "[Errno 7] Argument list too long", "rc": 7

我如何將ansible中的大型JSON結果傳遞到python腳本中?

我的第一個想法是將結果保存到文件,然后在python腳本中使用該文件。 但是在我看來,必須有更好的方法...

如果您的腳本能夠讀取STDIN的輸入,請嘗試使用here文檔

- name: "The Playbook"
  hosts: "localhost"

  tasks:
    - name: "Get JSON from API"
      uri:
        url: "https://my.real.api.url.goes.here.com"
        method: GET
        return_content: yes
        headers:
          Content-Type: "application/json"
          Authorization: "my api token goes here and works."
      register: result

    - name: Run Py script
      shell: |
        python get_tenants.py <<EOF
        "{{ result.content }}"
        EOF
      become: yes
      become_user: root
      register: pyout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM