简体   繁体   中英

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

I get an 'Argument list too long' error when passing very large json result to python script from within an Ansible playbook.

The base playbook is:

- 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

I've also tried passing the result as:

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

which passes a pointer to a map but not the map itself as a single item.

I've also tried passing the result as:

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

Regardless, I get:

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

How would I go about passing a large JSON result from ansible into a python script?

The first idea I had was to save the results to file and then use the file within the python script. It seems to me, however, that there has to be a better way...

Try it with a here document if your script is able to read input from STDIN:

- 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM