[英]start the stopped AWS instances using ansible playbook
I am trying to stop/start
the particular group
of instances listed in the hosts
file under group [target]
. 我想stop/start
特定的group
中所列出的实例的hosts
下组文件[target]
。 following playbook works fine to stop the instances. 以下剧本可以很好地停止实例。
---
- hosts: target
remote_user: ubuntu
tasks:
- name: Gather facts
action: ec2_facts
- name: Stop Instances
local_action:
module: ec2
region: "{{region}}"
instance_ids: "{{ansible_ec2_instance_id}}"
state: stopped
But when I am trying to start these instances, it's not working as the ec2_facts
is not able to ssh
into the instances (since they are stopped now) and get the instance-ids
但是,当我尝试启动这些实例时,由于ec2_facts
无法ssh
进入实例(因为它们现在已停止)并获取instance-ids
,所以它不起作用
---
- hosts: target
remote_user: ubuntu
tasks:
- name: start instances
local_action:
module: ec2
region: "{{region}}"
instance_ids: "{{ansible_ec2_instance_id}}"
state: running
I have already seen the documentation which make use of dynamic inventory
file for hosts and the way of hard-coding the instance-ids
. 我已经看过使用dynamic inventory
文件作为主机的文档以及对instance-ids
进行硬编码的方法。 I want to start the instances whose IPs
are listed in the target
group of hosts
file. 我想启动其IPs
在hosts
文件target
组中列出的实例。
Got the solution, Following is the ansible-task
that worked for me. 得到了解决方案,以下是对我ansible-task
。
---
- name: Start instances
hosts: localhost
gather_facts: false
connection: local
vars:
instance_ids:
- 'i-XXXXXXXX'
region: ap-southeast-1
tasks:
- name: Start the feature instances
ec2:
instance_ids: '{{ instance_ids }}'
region: '{{ region }}'
state: running
wait: True
Here is the Blog post on How to start/stop ec2 instances with ansible 这是有关如何使用Ansible启动/停止ec2实例的博客文章
You have 2 options: 您有2个选择:
Option 1 选项1
Use AWS CLI to query the instance-id of a stopped instance using its IP or name. 使用AWS CLI通过其IP或名称查询已停止实例的实例ID。 For example, to query the instance id for a given instance name: 例如,要查询给定实例名称的实例ID:
shell: aws ec2 describe-instances --filters 'Name=tag:Name,Values={{inst_name}}' --output text --query 'Reservations[*].Instances[*].InstanceId'
register: inst_id
Option 2 选项2
Upgrade Ansible to version 2.0 (Over the Hills and Far Away) and use the new ec2_remote_facts module 将Ansible升级到2.0版(越过遥远的山脉)并使用新的ec2_remote_facts模块
- ec2_remote_facts:
filters:
instance-state-name: stopped
You should add gather_facts: False to prevent Ansible from trying to SSH into the hosts since they're not running: 您应该添加collect_facts:False,以防止Ansible由于未运行而尝试通过SSH进入主机:
- hosts: target
remote_user: ubuntu
gather_facts: false
If you need to gather facts after the instances have started up then you can use the setup module to explicitly gather facts after they have booted up. 如果您需要在实例启动后收集事实,则可以使用setup模块在实例启动后显式收集事实。
Edit: I just realized that the issue is that you're trying to access the ansible_ec2_instance_id fact that you can't get because the instance is down. 编辑:我刚刚意识到问题是您正在尝试访问ansible_ec2_instance_id事实,因为该实例已关闭而无法获得。 You might want to take a look at this custom module called ec2_lookup that will let you search fetch AWS instance IDs even when the instances are down. 您可能需要看一下名为ec2_lookup的自定义模块 ,即使当实例关闭时, 该模块也可以让您搜索获取AWS实例ID。 Using this you can get a list of the instances you're interested in and then start them up. 使用此功能,您可以获取感兴趣的实例的列表,然后启动它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.