我如何准确地获取 python 输出 ( URL) 并将其分配给 Ansible get_url , url: 模块

How do I grab a python output ( a URL) EXACTLY as it is AND assign it to Ansible get_url , url: Module

我有一个 python 脚本,总的来说只输出一行,很长 URL。例如:https://www.google.com/。现在,在我执行 WITHIN ansible python 脚本后,我尝试将输出注册到一个变量,然后继续使用 get_url 模块从 URL 下载文件。

- name: get url from python code
  script: get_url.py
  register: output 

 - debug: var=output

 - name: Use URL to download file
   get_url: 
     url: "((output}}"
     dest: "/path/example/"

但是,我收到此错误:请求失败:

如何从 python 输出中获取准确的 URL 并使用 ansible get_url 下载它? python结果只有一行,也就是URL,没有别的。

考虑到stdout_lines返回的URL是一个列表(用[]表示),我们可以用第一个元素[0]来访问它,即output.stdout_lines[0].

那么 get_url 任务应该使用相同的 URL:

- name: Use URL to download file
  get_url: 
    url: "{{ output.stdout_lines[0] }}"
    dest: "/path/example/"

请注意,这是基于 Python 脚本 returns 只有 1 URL 的假设。在这种情况下,您也可以使用 url: "{{ output.stdout }}"