ansible - Ansible 没有在文件之间传递变量
问题描述
我试图使用 Ansible 将一些 jinja2 模板放到一个目录中,例如path/from/*.j2 to path/to/*.txt
.
在我的./defaults/main.yml
:
---
test_var:
- a: 1
b: 2
- a: 10
b: 20
在我的./tasks/main.yml
:
---
- name: "Copy file"
include: copy-files.yml
with_nested:
- test_var
loop_control:
loop_var: test_loop
在我的./tasks/copy-files.yml
:
---
- name: "copy {{ test_loop }}"
template:
src: "{{ test_loop.0.a }}"
dest: "{{ test_loop.0.b }}"
我收到以下错误:
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'unicode object' has no attribute 'b'"}
然后我使用调试并看到变量丢失了。
task path: ./tasks/main.yml
Wednesday 06 February 2019 01:15:10 +0000 (0:00:00.286) 0:00:04.308 ****
ok: [localhost] => {
"msg": [
{
"a": 1,
"b": 2
},
{
"a": 10,
"b": 20
}
]
}
TASK [./ : Copy files] ********
task path: ./tasks/main.yml
Wednesday 06 February 2019 01:15:11 +0000 (0:00:00.064) 0:00:04.373 ****
TASK [./ : debug] *******************************
task path: ./tasks/copy-files.yml
Wednesday 06 February 2019 01:15:11 +0000 (0:00:00.089) 0:00:04.463 ****
ok: [localhost] => {
"msg": [
"a",
"b"
]
}
那么这里有什么问题呢?
ansible 2.1.0.0
解决方案
那么这里有什么问题呢?
有几件事在起作用。
最重要的是,您缺少with_nested:
;的 jinja 替换。我不知道为什么你甚至得到了“a”和“b”,因为这很明显是list
你们中的 a 被str
喂食的with_nested:
。我相信你想要with_nested: "{{ test_var }}"
。由于您使用的是令人难以置信的、令人不安的古老版本的 ansible,因此 ansible 可能“帮助”了您,但是现代版本不会将该名称自动强制转换为变量,因此请注意。
但是,即使修复它也不能解决您的问题,因为with_nested:
想要 a list
of list
,而不是 a list
of dict
; 正如您从精美手册中看到的那样,它有效地调用{{ with_nested[0] | product(with_nested[1]) }}
并且a的乘积是它dict
的一个,这解释了您所看到的“a”和“b”list
tuple
.keys()
如果您希望src
and分别dest
是 thea
和b
key 的值,请跳过伪装并以with_nested:
这种方式构造:
with_nested:
- '{{ test_var | map(attribute="a") | list }}'
- '{{ test_var | map(attribute="b") | list }}'
推荐阅读
- typescript - Angular 11在递归嵌套的层次结构对象中找到键并更新键的值
- powerbi - 在父表中选择一个值时在powerBI中自动生成行号或序列号
- c# - 如何将 TabItem Header 绑定到 FrameworkElementFactory 中的 TextBox?
- excel - VBA 代码显示旧文件“xslx”问题的错误?
- sql - SQL查找一列总数的百分比
- java - 任何建议如何在 Java 中为 String.replaceAll() 创建正则表达式?
- python - 如何在 Visual Studio 中使用 bio python
- javascript - jquery .animate 在我的网站上不起作用
- java - 仅指定 t 的 Java 多维数组
- ruby - 是否可以在 Windows 中安装 zxing_cpp ruby gem?