pythonansiblegeturl

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


I have a python script that in summary outputs only one line, a long URL. example: https://www.google.com/ . Now, after I execute the python script WITHIN ansible, i try to register the output to a variable, and then proceed to use get_url module to download the file from the 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/"

However, I get this error: Request failed: <urlopen error unknown url type It seems that the result is still being output as stdout: 'https....'

How do i grab the exact URL from that python output and use ansible get_url to download it ? The python result is only one line, and that is only the URL, nothing else.


Solution

  • Considering that the URL returned in stdout_lines is a list (denoted by []), we can access it with the first element [0], i.e. output.stdout_lines[0].

    Then the get_url task should use the same URL:

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

    Note that this based on an assumption that the Python script returns only 1 URL. You could also use url: "{{ output.stdout }}" in that case.