Ansible 如何从复制模块上的列表项中组合文件名?
在我的Ansible剧本中,我将此列表作为变量:Ansible 如何从复制模块上的列表项中组合文件名?,ansible,ansible-playbook,Ansible,Ansible Playbook,在我的Ansible剧本中,我将此列表作为变量: collections: [customers licenses system] 列表用于多个位置。 在一个地方,我需要复制包含我的数据的现有文件(即customers.json,licenses.json,system.json) 这不起作用: - copy: src="{{ item }}.json" dest=~/import/ with_items: "{{ collections }}" 它首先连接列表,然后连接我的文件扩
collections: [customers licenses system]
列表用于多个位置。在一个地方,我需要复制包含我的数据的现有文件(即
customers.json
,licenses.json
,system.json
)
这不起作用:
- copy: src="{{ item }}.json" dest=~/import/
with_items: "{{ collections }}"
它首先连接列表,然后连接我的文件扩展名,因此类似于文件/customers licenses system.json
这也不起作用:
- copy: src={{ item ~ ".json" }} dest=~/import/
with_items: "{{ collections }}"
在本例中,它忽略文件扩展名,第一项看起来像files/customers
有没有一种方法可以让它在不复制变量或重命名文件的情况下工作?问题是,当前定义的变量只是一个字符串,而不是3个项目的列表:
collections: [customers licenses system]
下面是一个快速示例来演示:
- hosts: localhost
vars:
collections: [customers licenses system]
tasks:
- debug: var=item
with_items: collections
上面的输出是:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers licenses system) => {
"item": "customers licenses system"
}
因此ansible将集合
视为一个包含一个项目的列表。定义列表的正确方法是:
collections: ['customers', 'licenses', 'system']
或者,您也可以这样定义它:
collections:
- customers
- licenses
- system
当您将集合
更改为其中一个集合时,上述测试的输出将变为:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers) => {
"item": "customers"
}
ok: [localhost] => (item=licenses) => {
"item": "licenses"
}
ok: [localhost] => (item=system) => {
"item": "system"
}
更改列表的定义方式,复制模块应按预期工作。问题在于,当前定义的此变量只是一个字符串,而不是包含3项的列表:
collections: [customers licenses system]
下面是一个快速示例来演示:
- hosts: localhost
vars:
collections: [customers licenses system]
tasks:
- debug: var=item
with_items: collections
上面的输出是:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers licenses system) => {
"item": "customers licenses system"
}
因此ansible将集合
视为一个包含一个项目的列表。定义列表的正确方法是:
collections: ['customers', 'licenses', 'system']
或者,您也可以这样定义它:
collections:
- customers
- licenses
- system
当您将集合
更改为其中一个集合时,上述测试的输出将变为:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers) => {
"item": "customers"
}
ok: [localhost] => (item=licenses) => {
"item": "licenses"
}
ok: [localhost] => (item=system) => {
"item": "system"
}
更改列表的定义方式,复制模块应按预期工作。问题在于,当前定义的此变量只是一个字符串,而不是包含3项的列表:
collections: [customers licenses system]
下面是一个快速示例来演示:
- hosts: localhost
vars:
collections: [customers licenses system]
tasks:
- debug: var=item
with_items: collections
上面的输出是:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers licenses system) => {
"item": "customers licenses system"
}
因此ansible将集合
视为一个包含一个项目的列表。定义列表的正确方法是:
collections: ['customers', 'licenses', 'system']
或者,您也可以这样定义它:
collections:
- customers
- licenses
- system
当您将集合
更改为其中一个集合时,上述测试的输出将变为:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers) => {
"item": "customers"
}
ok: [localhost] => (item=licenses) => {
"item": "licenses"
}
ok: [localhost] => (item=system) => {
"item": "system"
}
更改列表的定义方式,复制模块应按预期工作。问题在于,当前定义的此变量只是一个字符串,而不是包含3项的列表:
collections: [customers licenses system]
下面是一个快速示例来演示:
- hosts: localhost
vars:
collections: [customers licenses system]
tasks:
- debug: var=item
with_items: collections
上面的输出是:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers licenses system) => {
"item": "customers licenses system"
}
因此ansible将集合
视为一个包含一个项目的列表。定义列表的正确方法是:
collections: ['customers', 'licenses', 'system']
或者,您也可以这样定义它:
collections:
- customers
- licenses
- system
当您将集合
更改为其中一个集合时,上述测试的输出将变为:
TASK: [debug var=item] ********************************************************
ok: [localhost] => (item=customers) => {
"item": "customers"
}
ok: [localhost] => (item=licenses) => {
"item": "licenses"
}
ok: [localhost] => (item=system) => {
"item": "system"
}
更改列表的定义方式,复制模块应按预期工作