使用Ansible Playbook运行EC2实例

时间:2018-07-22 11:36:08

标签: amazon-ec2 ansible

我只想获取正在运行的EC2实例。下面的剧本抛出以下错误:

fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution."}

这是环境中的错误吗?

这是剧本。

---
- hosts: localhost
  connection: local
  gather_facts: false

  vars:
      region: us-west-1
  tasks:
    - name: Gathers facts (instance metadata) about remote hosts remote ec2 (all)
      ec2_instance_facts:
       region: "{{ region }}"
       filters:
          "tag:Name": "Security-VPC*"

      register: ec2_metadata
    - name: print ec2 facts of TEST TEST
      debug:
        msg: "{{ ec2_metadata.instances | selectattr('state','equalto','running') | list  }}"

1 个答案:

答案 0 :(得分:0)

在收集ec2实例事实时,应该像过滤标记一样过滤状态,并且在将事实传递到<bucket-name>.s3-website-<AWS-region>.amazonaws.com/hello 时,必须循环遍历debug

这给出了适合我的示例。

ec2_metadata.instances

我得到此输出(显然我的服务器与您的过滤器不匹配)

---
- name: test aws ec2
  hosts: localhost
  connection: local
  gather_facts: False

  vars:
    region: us-west-1
  tasks:

    - name: Gathers facts (instance metadata) about remote hosts remote ec2 (all)
      ec2_instance_facts:
        region: "{{ region }}"
        filters:
          "tag:Name": "Security-VPC*"
          instance-state-name: running
      register: ec2_metadata

    - debug:
        msg: "{{ item }}"
      with_items: "{{ ec2_metadata.instances }}"
...