0

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
testcodezz
  • 69
  • 6
  • 2
    If possible then share your working script – Sabil Aug 28 '21 at 19:59
  • 1
    In the `debug` output, did you find the URL directly in `output`? If not, then using `url: "(( output }}"` is not going to work. – seshadri_c Aug 29 '21 at 04:12
  • I did change the debug to debug: var=output.stdout_lines, and the debug gives me "output.stdout_lines: [ "https://google.com] , all i need it to do is take the same thing to the get_url module, but it takes all details about ansible output etc. – testcodezz Aug 29 '21 at 04:55

1 Answers1

1

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.

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • 1
    Hello everyone, the suggestion above to do url: "{{output.stdout}}" did work for me :) . It was able to grab only the URL , and the file was able to download. I do not understand completely how the .stdout helped it grab only the URL, if anyone can explain this would be great to me and anyone else looking at this question in the future :) . Thank you again . – testcodezz Sep 02 '21 at 17:04
  • The `stdout` return value contains the standard output of the command. Text is separated by `\n` for newlines. The same will be conveniently split into a list in `stdout_lines`. – seshadri_c Sep 02 '21 at 17:31