将列表变量附加到 Ansible 中的另一个列表 [英] Append list variable to another list in Ansible
问题描述
是否可以将变量列表附加到 ansible 中的静态列表?
is it possible to append a variable list to a static list in ansible?
我可以将整个列表定义为一个变量:
I can define the whole list as a variable:
my_list:
- 1
- 2
- 3
然后在剧本中使用它
something: {{my_list}}
但我似乎无法找到如何执行此操作(伪代码):
But I cannot seem to find how to do this (pseudo code):
list_to_append:
- 3
- 4
然后在剧本中:
something:
- 1
- 2
- {{append: list_to_append}}
如果这实际上是不可能的,您对我的用例有何建议?
If that is in fact impossible, what would you suggest for my use case?
我在参数中有一个项目列表,但其中一些是可选
,应该可以使用变量进行修改.
I have a list of items in a parameter, but some of them are optional
and should be modifiable using variables.
换句话说:我有 default values
+ optional values
可以或不能通过变量添加.
In other words: I have default values
+ optional values
that could or could not be added via variables.
可选值
事先未知,我可以添加 1、2 或 100 个,因此它们不是静态的.
The optional values
are not known in advance, I could add 1, 2 or 100 of them, so they are not static.
我基本上有一个默认的静态列表 ++ 一个要附加的可配置变量列表.
I basically have a default static list ++ a configurable variable list to append.
我找到了这个,但它仅适用于 with_items,我需要在普通参数中使用它:
edit: I found this but it's only for with_items and I need it in a normal parameter:
with_flattened:
- "{{list1}}"
- "{{list2}}"
推荐答案
如果你真的想附加到内容,你需要使用 set_fact
模块.但是如果你只想使用合并的列表,就像这样简单:
If you really want to append to content, you will need to use the set_fact
module. But if you just want to use the merged lists it is as easy as this:
{{ list1 + list2 }}
使用 set_fact
看起来像这样:
With set_fact
it would look like this:
- set_fact:
list_merged: "{{ list1 + list2 }}"
注意:如果您需要对串联列表进行额外的操作,请确保将它们分组如下:
NOTE: If you need to do additional operations on the concatenated lists be sure to group them like so:
- set_fact:
list_merged: "{{ (list1 + list2) | ... }}"
这篇关于将列表变量附加到 Ansible 中的另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!