使用ansible中的with_items获取多个cloudformation堆栈的cloudformation_facts

时间:2016-11-17 15:06:27

标签: ansible ansible-facts

我有多个clouformation堆栈,并将其名称存储为CF_TEMPLATE_ITEMS

中的列表

现在我正在尝试收集有关所有这些信息的信息(最后我希望拥有所有这些信息的stack_output):

- name: Get all facts for all cf stacks
  cloudformation_facts:
    stack_name: "{{ item }}"
  with_items: "{{ CF_TEMPLATE_ITEMS }}"

不幸的是,之后cloudformation仅包含 last 堆栈的信息。似乎其他人的信息被覆盖了。

我可以以某种方式从堆栈名称列表中捕获所有云形式堆栈的事实吗?

1 个答案:

答案 0 :(得分:4)

是的,cloudformation_facts会在每次运行时覆盖cloudformation个事实。

要从每次运行中收集数据,register循环结果并将其重新格式化为干净的字典,如下所示:

- cloudformation_facts:
    stack_name: "{{ item }}"
  with_items: "{{ CF_TEMPLATE_ITEMS }}"
  register: cf_tmp
- set_fact:
    cf: "{{ dict(cf_tmp.results | map(attribute='ansible_facts.cloudformation') | map('dictsort') | sum(start=[])) }}"

此代码未经过测试。这应该为您提供cf dict,并将所有堆栈事实作为其密钥。