ansible - 如何将循环的输出用作单行?

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

我的目标是有一个剧本,它读取一个包含端点列表的 txt 文件并每行执行 curl 命令

我正在读取一个文件行,其中有一些 url 与下面的任务有关,它工作得很好

- 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"
}

现在我希望使用该输出的每一行作为 url 我应该检查我的任务,如下所示。

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

但是我得到的是一个包含所有文件行的列表

"['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']"}

如何让每一行成为一个单独的字符串并将其用作任务中 url 字段的值?

非常欢迎任何想法、意见、建议,干杯。

您必须使用与 debug 任务相同的方法。由于 url 参数采用字符串,因此您使用 url: "{{ item }}".

任务应如下所示:

- 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 }}"