使用ansible playbook运行EC2实例

问题描述 投票:0回答:1

我想只运行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  }}"
amazon-ec2 ansible
1个回答
0
投票

你应该像收集标签一样收集ec2实例事实时过滤状态,并且当将事实传递给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 }}"
...

我得到这个输出(显然我的服务器与你的过滤器不匹配)

$ ansible-playbook ec2_instance_facts.yml 

PLAY [test aws ec2] ****************************************************************************************************************************

TASK [Gathers facts (instance metadata) about remote hosts remote ec2 (all)] **************************************************
ok: [localhost]

TASK [debug] ***************************************************

PLAY RECAP ***************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   
© www.soinside.com 2019 - 2024. All rights reserved.