Amazon ec2 使用ansible playbook运行EC2实例

Amazon ec2 使用ansible playbook运行EC2实例,amazon-ec2,ansible,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:

我只希望运行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  }}"

在收集ec2实例事实时,应该像对标记所做的那样过滤状态,在将事实传递到
debug
时,必须循环使用
ec2\u元数据。实例

这是一个适合我的例子

---
- 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 }}"
...
我得到这个输出(显然我的服务器与你的过滤器不匹配)


您好,我知道这是您关于Stackoverflow的第一个问题,请阅读并编辑该问题以提供缺少的信息(不要在评论中回答)。改进示例:ansible的版本…您还可以说错误出现在哪些任务中。您是否尝试使用
--语法检查
查看代码中的任何潜在错误。谢谢。如果我需要实例id,下面的脚本不起作用。有什么问题吗调试:msg:“{item.instance_id}}”和{u 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