简体   繁体   English

使用ansible剧本启动停止的AWS实例

[英]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. 我想启动其IPshosts文件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.

相关问题 使用 Ansible playbook 启动和停止 AWS RDS - Start and Stop AWS RDS's with Ansible playbook 无法使用Ansible剧本删除AWS VPC - Not able to Delete AWS VPC using ansible playbook 如何在Ansible中启动所有AWS EC2实例 - How can I start all AWS EC2 instances in Ansible 如何在ansible playbook中动态使用AWS实例的IP地址,而不使用ansible playbook将其保存在外部文件中 - how to use AWS instance's IP address dynamically in an ansible playbook without saving it in an external file using ansible playbook 如何在AWS Lambda函数中运行Ansible Playbook - how to run ansible playbook in aws lambda function 使用Ansible自动配置AWS自动缩放组实例 - Using Ansible to automatically configure AWS autoscaling group instances 使用具有主从配置的Ansible-Pull对AWS实例进行自动扩展 - Autoscaling with AWS instances using Ansible-Pull with Master and slave Configuration 使用Ansible手册在AWS(Amazon)ec2中部署Play Framework应用程序 - To deploy Play Framework apps in AWS (Amazon) ec2 using Ansible playbook 如何在 ansible-playbook 中查找 amazon.aws.aws_secret? - How to lookup an amazon.aws.aws_secret in ansible-playbook? 是否可以从Chef AWS / Opworks食谱中运行Ansible剧本? - Is it possible to run an Ansible playbook from a Chef AWS/Opworks cookbook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM