farp332
farp332

Reputation: 746

ansible - How to use output from loop as a single line?

My goal is to have a playbook that read a txt file with a list of endpoints and execute curl command per each line

I'm reading a file lines that has some url's with the task below and it works just fine

- debug: msg="{{item}}"
  loop: "{{ lookup('file', './endpoints.txt').splitlines() }}"
TASK [debug] ************************************************************************************************************************************************
ok: [localhost] => (item=http://test01.net/index.html) => {
    "msg": "http://test01.net/index.html"
}
ok: [localhost] => (item=http://test02.net/index.html) => {
    "msg": "http://test02.net/index.html"
}
ok: [localhost] => (item=http://test03.net/index.html) => {
    "msg": "http://test03.net/index.html"
}
ok: [localhost] => (item=http://test04.net/index.html) => {
    "msg": "http://test04.net/index.html"
}
ok: [localhost] => (item=http://test05.net/index.html) => {
    "msg": "http://test05.net/index.html"
}
ok: [localhost] => (item=http://test06.net/index.html) => {
    "msg": "http://test06.net/index.html"
}
ok: [localhost] => (item=http://test07.net/index.html) => {
    "msg": "http://test07.net/index.html"
}

Now I'm hoping to use every line of that output as the url I supposed to check with my task, something like below.

- name: Check that you can connect (GET) to a page and it returns a status 200
  uri:
    url: "{{ lookup('file', './endpoints.txt').splitlines() | list }}"

However what I get is a list with all the file lines

"['http://test01.net/index.html', 'http://test02.net/index.html', 'http://test03.net/index.html', 'http://test04.net/index.html', 'http://test05.net/index.html', 'http://test06.net/index.html', 'http://test07.net/index.html']**
TASK [Check that you can connect (GET) to a page and it returns a status 200] *******************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "elapsed": 0, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error unknown url type: ['http>", "redirected": false, "status": -1, "url": "['http://test01.net/index.html', 'http://test02.net/index.html', 'http://test03.net/index.html', 'http://test04.net/index.html', 'http://test05.net/index.html', 'http://test06.net/index.html', 'http://test07.net/index.html']"}

How can I make every line a separate string and use it as value for the url field in my task?.

Any ideas, input, advises are very welcome, cheers.

Upvotes: 0

Views: 822

Answers (1)

seshadri_c
seshadri_c

Reputation: 7340

You'll have to use the same method, that you used for your debug task. Since the url parameter takes a string, you use url: "{{ item }}".

The task should look like:

- name: Check that you can connect (GET) to a page and it returns a status 200
  uri:
    url: "{{ item }}"
  loop: "{{ lookup('file', './endpoints.txt').splitlines() | list }}"

Upvotes: 1

Related Questions