代码之家  ›  专栏  ›  技术社区  ›  Old Schooled

如何将ansible play中的大型json结果传递到python脚本中

  •  0
  • Old Schooled  · 技术社区  · 7 年前

    基本剧本是:

    - 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脚本中使用该文件。然而,在我看来,必须有更好的方法。。。

    1 回复  |  直到 7 年前
        1
  •  1
  •   JGK    7 年前

    试着用一个 here document 如果脚本能够从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