如何使用ansible发布AWS弹性IP?

How to release an AWS elastic IP with ansible?

我正在关注此处的 ansible 文档:https://docs.ansible.com/ansible/latest/modules/ec2_eip_module.html 以便为 ec2 实例提供新的弹性 IP。参数release_on_disassociationset为yes但解除关联后弹性IP未释放

首先,我用弹性 IP 创建了 ec2:

- name: provision new instances with ec2
  ec2:
    keypair: mykey
    instance_type: c1.medium
    image: ami-40603AD1
    wait: yes
    group: webserver
    count: 3
  register: ec2

- name: associate new elastic IPs with each of the instances
  ec2_eip:
    device_id: "{{ item }}"
    release_on_disassociation: yes
  loop: "{{ ec2.instance_ids }}"

之后解除弹性IP:

- name: Gather EC2 facts
  ec2_instance_facts:
    region: "{{ region }}"
    filters:
      "tag:Type": "{{ server_type }}"
  register: ec2
- name: disassociate an elastic IP with a device
  ec2_eip:
    device_id: '{{ item.instance_id }}'
    ip: '{{ item.public_ip_address }}'
    state: absent
  when: item.public_ip_address is defined
  with_items: "{{ ec2.instances }}"

ansible --version

ansible 2.8.4

Python 版本为 3.7.4

我认为 release_on_disassociation: 可能是 AWS 的一项功能,但即使是,对您的情况也无关紧要,因为该模块在 state: present 操作期间不会检查该参数。相反,它仅参考该参数 during state: absent

所以我认为您需要将该参数从顶部 ec2_eip 移到底部:

- name: disassociate an elastic IP with a device
  ec2_eip:
    device_id: '{{ item.instance_id }}'
    ip: '{{ item.public_ip_address }}'
    release_on_disassociation: yes
    state: absent
  when: item.public_ip_address is defined
  with_items: "{{ ec2.instances }}"