【问题标题】:ansible ec2_instance_facts filter by "tag:Name" does not filter by instance Nameansible ec2_instance_facts 按“tag:Name”过滤不按实例名称过滤
【发布时间】:2018-10-18 21:21:31
【问题描述】:

我想运行 ec2_instance_facts 以按名称查找实例。但是,我一定是做错了什么,因为我无法让过滤器真正工作。以下返回我设置的 AWS_REGION 中的所有内容:

- ec2_instance_facts:
  filters:
    "tag:Name": "{{myname}}"
  register: ec2_metadata

- debug: msg="{{ ec2_metadata.instances }}"

【问题讨论】:

    标签: amazon-web-services amazon-ec2 ansible


    【解决方案1】:

    答案是使用ec2_remote_facts 模块,而不是ec2_instance_facts 模块。

    - ec2_remote_facts:
        filters:
          "tag:Name": "{{myname}}"
      register: ec2_metadata
    
    - debug: msg="{{ ec2_metadata.instances }}"
    

    【讨论】:

      【解决方案2】:

      根据文档,ec2_remote_facts 从 ansible 版本 2.8 被标记为已弃用,以支持使用 ec2_instance_facts

      这对我很有用:

      - name: Get instances list
        ec2_instance_facts:
          region: "{{ region }}"
          filters:
            "tag:Name": "{{ myname }}"
        register: ec2_list
      
      - debug: msg="{{ ec2_metadata.instances }}"
      

      也许过滤器没有被应用?能不能遍历一下对象中的结果?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多