如何在键来自变量的情况下在ansible中进行CSV查找?

How to do CSV lookups in ansible where the key comes from a variable?

因此 ansible 可以从 CSV 文件中查找内容,他们网页上的示例是:

- debug: msg="The atomic number of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=,') }}"
- debug: msg="The atomic mass of Lithium is {{ lookup('csvfile', 'Li file=elements.csv delimiter=, col=2') }}"

现在,我的 CSV 文件包含主机名到数字的映射,如下所示:

HOST,ID
foo,0
bar,1

现在,当我将其改编为:

- debug: msg="My ID is {{ lookup('csvfile', '{{ inventory_hostname }} file=my.csv delimiter=,') }}"

我收到错误:

Failed to template msg="My ID is {{ lookup('csvfile', '{{ inventory_hostname }} file=my.csv delimiter=,') }}": need more than 1 value to unpack

我该怎么做?

使用字符串格式

- debug: msg="My ID is {{ lookup('csvfile', '{} file=my.csv delimiter=,'.format(inventory_hostname)) }}"