首页 > 解决方案 > 使用 Ansible 创建 Aws ec2 实例时出错

问题描述

我已经运行了简单的任务来创建 aws 实例。

之后创建了实例,我从 AWS Web 控制台终止了 ec2 实例。后来我向它添加了更多任务,但永远无法使用这些任务创建 ec2 实例。

我不断收到错误消息:

致命的:[本地主机]:失败!=> {"changed": false, "msg": "id(s) ['i-0f513aec91787e83d'] 的实例之前已创建但已被终止 - 使用(可能不同的)'instance-id' 参数"}

它不断报告先前的实例 ID。我可以看到我的 AWS 控制台中没有这样的实例。

有人可以向我解释为什么会这样吗?

- name: get any running ec2 instance
  ec2_instance_info:
    aws_access_key: "{{ec2_access_key}}"
    aws_secret_key: "{{ec2_secret_key}}"
    region: "{{region}}"
    filters:
      instance-state-name: ['running']
  register: ec2_list

- name: Display ec2_list
  debug: msg="{{item.instance_id}}"
  with_items: "{{ec2_list.instances}}"


- name: Terminate Any running Instances
  ec2:
    state: 'absent'
    instance_ids: "{item.instance_id}"
  with_items: "{{ec2_list.instances}}"


- name: Create new ec-2 instance
  ec2:
    aws_access_key: "{{ec2_access_key}}"
    aws_secret_key: "{{ec2_secret_key}}"
    key_name: "{{key_name}}"
    id: "{{id}}"
    instance_type: t2.micro
    image: "{{image}}"
    region: "{{region}}"
    vpc_subnet_id: "{{vpc_subnet_id}}"
    wait: yes
    count: 1
    assign_public_ip: yes
  register: ec2

- name: Wait for ssh to come up
  delegate_to: "{{ item.public_dns_name }}"
  wait_for_connection:
    delay: 60
    timeout: 320
    loop: "{{ ec2.instances }}"

- name: Terminate currently running Instances
  ec2:
    state: 'absent'
    instance_ids: "{{ec2.instance_ids}}"

标签: amazon-web-servicesamazon-ec2ansibleboto3ansible-facts

解决方案


ec2 ansible 模块的文档说:

id:此标识符在实例终止后至少 24 小时内有效,以后不应再用于其他调用。

你改变了这个id吗?


推荐阅读