我已经创建了一个代码,它成功地将我的实例与EIP相关联。但是,我的public_ip addr保持不变,即在与弹性ip关联之前给虚拟机的地址。这是代码,
---
- name: Provision an EC2 Instance
hosts: local
connection: local
gather_facts: False
tags: provisioning
vars:
instance_type: t2.micro
security_group: My secure group
image: ami-5b673c34
keypair: mynodes
region: ap-south-1
count: 1
tasks:
- name: Launch the new EC2 Instance
local_action: ec2
group={{ security_group }}
instance_type={{ instance_type}}
image={{ image }}
wait=true
region={{ region }}
keypair={{ keypair }}
count={{count}}
register: ec2
- name: Get the Ip address
debug: var=ec2.instances[0].public_dns_name
- name: Allocating elastic IP to instance
ec2_eip:
in_vpc: yes
reuse_existing_ip_allowed: yes
state: present
region: "{{ region }}"
device_id: "{{ item }}"
with_items: "{{ ec2.instance_ids }}"
#device_id: "{{ ec2.instances[0] }}"
#register: instance_eip
- debug: msg=ec2.instances[0].public_ip
- #name: eip with instance
#pause:
#seconds: 30
- name: Add the newly created EC2 instance(s) to the local host group
local_action: lineinfile
dest="/etc/ansible/hosts"
regexp={{ item.private_ip }}
insertafter="[node3]" line={{ item.private_ip }}
with_items: "{{ ec2.instances }}"
- name: Wait for SSH to come up
local_action: wait_for
host={{ item.public_ip }}
port=22
state=started
with_items: '{{ ec2.instances }}'
- name: Add tag to Instance(s)
local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
with_items: '{{ ec2.instances }}'
args:
tags:
Name: node3
因此,如果我没有为其分配弹性ip,则我的代码当前运行良好。但是当我这样做时,我就无法执行将ssh'ing到新创建的虚拟机中的任务。