首页 > 解决方案 > 如何在ansible中打印python请求响应

问题描述

嗨,我正在使用 ansible 运行 python 脚本,该脚本在从 url 获取数据后打印 json 响应。运行 ansible 任务(在 ansible 输出中)后如何查看 json 输出?

这是我的剧本

----
- name: Run on remote host
hosts: localhost

tasks:
 - name: run python script locally
command: python test.py -argument1 value -argument value

这是我的python脚本

import pprint
import requests
import base64

pat = ""
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')),   
'ascii')
url="https://dev.azure.com/{org}/_apis/projects?api-version=5.1"

headers = {
'Accept': 'application/json',
'Authorization': 'Basic '+authorization
}

response = requests.get(url, headers=headers)
if response.status_code == 200:
   print('Success!')
elif response.status_code == 404:
   print('Not Found.')
data = response.json()
pprint.pprint(data) 

标签: pythonansiblepython-requests

解决方案


推荐阅读