是否可以在 Ansible 中设置数组的事实?

问题描述:

是否可以使用 set_fact 在 ansible 中设置包含数组的事实?它的正确语法是什么?

Is it possible to set a fact containing an array in ansible using set_fact? What's the correct syntax for it?

确实如此.不过,您需要引用整个数组:

Indeed it is. You need to quote the entire array though:

- name: set fact
  set_fact: foo="[ 'one', 'two', 'three']"

- name: debug
  debug: msg={{ item }}
  with_items: foo

上述任务应生成以下输出:

The above tasks should generate the following output:

TASK: [set fact] **************************************************************
ok: [localhost]

TASK: [debug] *****************************************************************
ok: [localhost] => (item=one) => {
    "item": "one",
    "msg": "one"
}
ok: [localhost] => (item=two) => {
    "item": "two",
    "msg": "two"
}
ok: [localhost] => (item=three) => {
    "item": "three",
    "msg": "three"
}