首页 > 解决方案 > 如何使用ansible将标签添加到弹性IP

问题描述

如何将带有 ansible 的标签添加到分配的弹性 IP。官方文档中没有信息:

https://docs.ansible.com/ansible/latest/modules/ec2_eip_module.html

示例代码如下:

- 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 }}"
  loop: "{{ ec2.instance_ids }}"

如果我添加标签字段,则会引发以下错误:

失败:[localhost] (item=i-08d2c1fee9eef9001) => {"ansible_loop_var": "item", "changed": false, "item": "i-08d2c1fee9eef9001", "msg": "(ec2_eip) 不支持的参数模块:标签支持的参数包括:allow_reassociation、aws_access_key、aws_secret_key、debug_botocore_endpoint_logs、device_id、ec2_url、in_vpc、private_ip_address、profile、public_ip、region、release_on_disassociation、reuse_existing_ip_allowed、security_token、state、validate_certs、wait_timeout"}

标签: amazon-web-servicesansible

解决方案


您可以使用 ec2_tag 模块来标记 EC2 资源。

https://docs.ansible.com/ansible/latest/modules/ec2_tag_module.html#ec2-tag-module

- name: associate new elastic IP with instance
  ec2_eip:
    device_id: "{{ l_ec2_instance_id }}"
  register: l_eip_register

- name: tag the elastic IP
  ec2_tag:
    resource: "{{ l_eip_register.allocation_id }}"
    tags:
      Name: webserver

推荐阅读