如何在ansible中从实例id获取ec2实例ip

时间:2016-07-12 01:57:38

标签: amazon-web-services amazon-ec2 ansible

我正在使用ansible cloudformation来创建包含20个实例的堆栈。 现在在ansible输出中我只能看到实例ID。

现在创建堆栈后,我想连接到它们并进行配置,但我不确定如何从实例id获取thos ips或主机名。

cloudformation输出就像这样

Meteor.users.update( {_id: Meteor.userId() }, {
  $set: {'profile.avatar': avatarUrl}
});

2 个答案:

答案 0 :(得分:2)

ec2_remote_facts模块是你的朋友。

答案 1 :(得分:0)

当Ansible在实例上运行时,您可以检索instance meta data,例如

curl http://169.254.169.254/latest/meta-data/public-hostname
ec2-aa-bb-cc-ddd.ap-southeast-2.compute.amazonaws.com

其中aa-bb-cc-ddd表示您的IP,完整字符串表示主机名。

您正在使用Ansible,因此您可以使用Ansible的get_url模块:http://docs.ansible.com/ansible/get_url_module.html来执行HTTP请求。