【问题标题】:Ansible: Get instance_ids from multiple stacksAnsible:从多个堆栈中获取 instance_ids
【发布时间】:2021-08-03 10:15:11
【问题描述】:

我正在尝试从不同的堆栈中获取 instance_id 的列表:

---
- hosts: localhost

  vars:
    stack_names:
      - 'stacka'
      - 'stackb'
      - 'stackc'

  tasks:
  - name: "get cloudformation facts from stacks"
    cloudformation_facts:
      stack_name: "{{ item }}"
      stack_resources: true
    with_items: "{{ stack_names }}"
    register: cf_tmp

  - name: "Get list of instance_ids"
    set_fact:
      instance_ids: "{{ dict(cf_tmp.results | map(attribute=ansible_facts['cloudformation'][stack_name]['stack_resources']['instance'])) }}"

  - debug:
      msg: "{{ instance_ids }}"

但出现以下错误:

TASK [Get list of instance_ids] *************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'stack_name' is undefined

无论如何要解决这个问题?

谢谢!

【问题讨论】:

  • 我在您的示例中的任何地方都没有看到 stack_name 定义(如您的错误消息所报告的那样)。你的意思是'stack_name'?此外,应在表达式中引用整个属性名称。由于不是每个人都可以访问 cloudfotmatiin 进行测试,因此您应该举例说明寄存器事实和确切的预期结果。
  • @jackw11111,更改为 cloudformation_facts 会导致类似的错误,而不是“stack_name”,而是“错误是:“dict object”没有属性“cloudformation”。似乎两者都是单栈。
  • 您的示例可能需要stack_namesee this documentationstack_name 是必需的。

标签: loops ansible


【解决方案1】:

最终使用角色作为解决方法来完成工作。

#role
  - name: "get cloudformation facts from stacks"
    cloudformation_info:
      stack_name: "{{ stack_name }}"
      stack_resources: true
    register: cf_tmp

  - name: get instance id from stack
    set_fact:
      instance_id: "{{ cf_tmp.cloudformation[stack_name].stack_resources.Instance }}"
roles:
    - role: GetInstanceId
      vars:
        stack_name: "stacka"

    - role: GetInstanceId
      vars:
        stack_name: "stackb"

    - role: GetInstanceId
      vars:
        stack_name: "stackc"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2011-01-19
    相关资源
    最近更新 更多