从剧本中的多个主机收集变量

Collecting variables from multiple hosts in a playbook

我有一个从多个主机收集信息列表的剧本,我想将这些列表一起收集到一个变量中。 因此,例如,我的输出可能如下所示:

ok: [ALBSWA052] => {
    "myvar": [
        "ALBSWA052 exists",
        "ALBSWA052 is secondary"
    ]
}
ok: [ALBSWA051] => {
    "myvar": [
        "ALBSWA051 exists",
        "ALBSWA051 is primary"
    ]
}

我想将它们组合成一个列表,例如

    "myvar": [
        "ALBSWA052 exists",
        "ALBSWA052 is secondary"
        "ALBSWA051 exists",
        "ALBSWA051 is primary"
    ]

有没有优雅的方法来做到这一点?我能想到的唯一方法就是将它写入文件并读回。

例如

    - set_fact:
        myvar: "{{ ansible_play_hosts|map('extract', hostvars, 'myvar')|flatten }}"
      run_once: true

给出了预期的结果

myvar:
  - ALBSWA051 exists
  - ALBSWA051 is secondary
  - ALBSWA052 exists
  - ALBSWA052 is secondary