检查 EC2 实例是否存在 运行 然后跳过部署

Check if EC2 instances exists and running then skip deployment

我正在尝试 运行 一个调用角色来部署一些 EC2 实例的剧本,一切正常,除了我想在 EC2 实例存在并且在 运行ning 时设置条件跳过部署的状态 我使用以下命令检索 ec2_infos :

## Check if an instance with same name exist on AWS
- name: Get {{ ec2_name }} infos
  ec2_instance_info:
    region: "us-east-1"
    filters:
      "tag:Name": "{{ ec2_name }}"
      instance-state-name: [ "running"]
  register: ec2_infos

- name: DEBUG
  debug: msg="{{ aws_ec2_infos }}"

在部署阶段我的情况如下:


- name: "{{ ec2_description }} - {{ ec2_name }}"
  cloudformation:
    stack_name: "some name "
    state: "present"
    region: "{{ aws_region }}"
    template: "PATH/ec2.json"
    template_parameters:
      Name: "{{ ec2_name }}"
      Description: "{{ ec2_description }}"
      KeyName: "{{key_name }}"
      KmsKeyId: "{{ key_id }}"
      GroupSet: "{{ id }}"
      IamInstanceProfile: "{{ name }}"
      Type: "OS"
  **when: ec2_infos.state[0].name != 'running'**

但我收到一条错误消息:

"msg": "The conditional check 'aws_ec2_infos.state[0].name != 'running'' failed. The error was: error while evaluating conditional (aws_ec2_infos.state[0].name != 'running'): 'dict object' has no attribute 

我想我在我的情况下遗漏了一些东西,但我找不到确切的东西。任何提示或建议都非常受欢迎

正如@benoit 和@mdaniel 所说,错误是在我的理解中,条件应该是:

aws_ec2_infos.instances[0].state.name != 'running'