简体   繁体   English

如何将Ansible Play中的大型JSON结果传递到python脚本中

[英]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. 从Ansible剧本中将非常大的json结果传递到python脚本时,出现“参数列表过长”错误。

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? 我如何将ansible中的大型JSON结果传递到python脚本中?

The first idea I had was to save the results to file and then use the file within the python script. 我的第一个想法是将结果保存到文件,然后在python脚本中使用该文件。 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: 如果您的脚本能够读取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