将列表变量追加到Ansible中的另一个列表 [英] Append list variable to another list in 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
,然后在剧本中将其用作
and then use it in a playbook as
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?
我在参数中有一个项目列表,但是其中一些是optional
,应该可以使用变量进行修改.
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.
optional values
事先未知,我可以添加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屋!