如何使用表达式设置结构化变量
How to set a structured variable with an expression
Ansible 中是否有任何方法可以将变量设置为由表达式构造的某些结构化值(例如列表或字典)?
例如,我有这些变量:
all_flavours: ["vanilla", "chocolate", "strawberry", "banana"]
flavours_to_keep: ["chocolate", "banana"]
我想计算表达式 all_flavours | difference(flavours_to_keep)
并将生成的结构化集合(列表或集合)存储在变量 flavours_to_drop
中,这样对于这些值就好像我定义了:
flavours_to_drop: ["vanilla", "strawberry"]
但是,我找不到执行此操作的语法。这是我的尝试:
尝试 1
flavours_to_drop: all_flavours | difference(flavours_to_keep)
这被简单地解释为一个字符串:
flavours_to_drop: "all_flavours | difference(flavours_to_keep)"
尝试 2
flavours_to_drop: {{ all_flavours | difference(flavours_to_keep) }}
这是一个错误,因为 YAML 将“{”解释为字典。
尝试 3
flavours_to_drop: "{{ all_flavours | difference(flavours_to_keep) }}"
这会正确地设置差异,然后将其转换为字符串,所以我最终得到:
flavours_to_drop: "set([\"vanilla\", \"strawberry\"])"
如何计算表达式,而不是将结构化对象直接存储回变量而不是将其转换为字符串?
我应该补充一点,我使用的是 Ansible 版本 1.6.2。此行为是否在 Ansible 版本之间发生了变化?
格式等可能有点棘手。您需要引用过滤器才能正确解析它。您可以将其作为 var 或在 set_fact 中执行。你想要这样的东西:
vars:
all_flavours: ["vanilla", "chocolate", "strawberry", "banana"]
flavours_to_keep: ["chocolate", "banana"]
flavours_to_drop_1: "{{ all_flavours | difference(flavours_to_keep) }}"
tasks:
- debug: var=all_flavours
- debug: var=flavours_to_keep
- set_fact:
flavours_to_drop_2: "{{ all_flavours | difference(flavours_to_keep) }}"
- debug: var=flavours_to_drop_1
- debug: var=flavours_to_drop_2
以上结果为:
TASK: [debug var=all_flavours] ************************************************
ok: [localhost] => {
"all_flavours": [
"vanilla",
"chocolate",
"strawberry",
"banana"
]
}
TASK: [debug var=flavours_to_keep] ********************************************
ok: [localhost] => {
"flavours_to_keep": [
"chocolate",
"banana"
]
}
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [debug var=flavours_to_drop_1] ********************************************
ok: [localhost] => {
"flavours_to_drop_1": [
"vanilla",
"strawberry"
]
}
TASK: [debug var=flavours_to_drop_2] ********************************************
ok: [localhost] => {
"flavours_to_drop_2": [
"vanilla",
"strawberry"
]
}
Ansible 中是否有任何方法可以将变量设置为由表达式构造的某些结构化值(例如列表或字典)?
例如,我有这些变量:
all_flavours: ["vanilla", "chocolate", "strawberry", "banana"]
flavours_to_keep: ["chocolate", "banana"]
我想计算表达式 all_flavours | difference(flavours_to_keep)
并将生成的结构化集合(列表或集合)存储在变量 flavours_to_drop
中,这样对于这些值就好像我定义了:
flavours_to_drop: ["vanilla", "strawberry"]
但是,我找不到执行此操作的语法。这是我的尝试:
尝试 1
flavours_to_drop: all_flavours | difference(flavours_to_keep)
这被简单地解释为一个字符串:
flavours_to_drop: "all_flavours | difference(flavours_to_keep)"
尝试 2
flavours_to_drop: {{ all_flavours | difference(flavours_to_keep) }}
这是一个错误,因为 YAML 将“{”解释为字典。
尝试 3
flavours_to_drop: "{{ all_flavours | difference(flavours_to_keep) }}"
这会正确地设置差异,然后将其转换为字符串,所以我最终得到:
flavours_to_drop: "set([\"vanilla\", \"strawberry\"])"
如何计算表达式,而不是将结构化对象直接存储回变量而不是将其转换为字符串?
我应该补充一点,我使用的是 Ansible 版本 1.6.2。此行为是否在 Ansible 版本之间发生了变化?
格式等可能有点棘手。您需要引用过滤器才能正确解析它。您可以将其作为 var 或在 set_fact 中执行。你想要这样的东西:
vars:
all_flavours: ["vanilla", "chocolate", "strawberry", "banana"]
flavours_to_keep: ["chocolate", "banana"]
flavours_to_drop_1: "{{ all_flavours | difference(flavours_to_keep) }}"
tasks:
- debug: var=all_flavours
- debug: var=flavours_to_keep
- set_fact:
flavours_to_drop_2: "{{ all_flavours | difference(flavours_to_keep) }}"
- debug: var=flavours_to_drop_1
- debug: var=flavours_to_drop_2
以上结果为:
TASK: [debug var=all_flavours] ************************************************
ok: [localhost] => {
"all_flavours": [
"vanilla",
"chocolate",
"strawberry",
"banana"
]
}
TASK: [debug var=flavours_to_keep] ********************************************
ok: [localhost] => {
"flavours_to_keep": [
"chocolate",
"banana"
]
}
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [debug var=flavours_to_drop_1] ********************************************
ok: [localhost] => {
"flavours_to_drop_1": [
"vanilla",
"strawberry"
]
}
TASK: [debug var=flavours_to_drop_2] ********************************************
ok: [localhost] => {
"flavours_to_drop_2": [
"vanilla",
"strawberry"
]
}