简体   繁体   中英

ansible ec2_instance_facts filter by “tag:Name” does not filter by instance Name

I want to run ec2_instance_facts to find an instance by name. However, I must be doing something wrong because I cannot get the filter to actually work. The following returns everything in my set AWS_REGION:

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

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

The answer is to use the ec2_remote_facts module, not the ec2_instance_facts module.

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

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

Based on the documentation ec2_remote_facts is marked as DEPRECATED from ansible version 2.8 in favor of using ec2_instance_facts .

This is working good for me:

- name: Get instances list
  ec2_instance_facts:
    region: "{{ region }}"
    filters:
      "tag:Name": "{{ myname }}"
  register: ec2_list

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

Maybe the filte is not being applied? Can you go through the results in the object?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM