Ajeet Khan
Ajeet Khan

Reputation: 9190

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]. 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

---
- 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. I want to start the instances whose IPs are listed in the target group of hosts file.

Upvotes: 9

Views: 3148

Answers (4)

Rikard
Rikard

Reputation: 409

Update on HelloV since ec2_remote_facts have been deprecated and I couldn't get ec2_instance_info or whatever it should be now to work on a stopped instance, I did the following

---
- name: Patch and reboot gocd
  hosts: all
  become: yes
  gather_facts: false
  tasks:

  - set_fact:
      inst_name: "{{ ansible_host }}"
  
  - name: get instance id
    delegate_to: localhost
    shell: aws ec2 describe-instances --filters 'Name=tag:Name,Values={{ inst_name }}' --output text --query 'Reservations[*].Instances[*].InstanceId'
    register: inst_id
  
  - set_fact:
      inst_id: "{{ inst_id.stdout }}"

Upvotes: 0

Ajeet Khan
Ajeet Khan

Reputation: 9190

Got the solution, Following is the ansible-task that worked for me.

---
- 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

Upvotes: 5

helloV
helloV

Reputation: 52423

You have 2 options:

Option 1

Use AWS CLI to query the instance-id of a stopped instance using its IP or name. For example, to query the instance id for a given instance name:

shell: aws ec2 describe-instances --filters 'Name=tag:Name,Values={{inst_name}}' --output text --query 'Reservations[*].Instances[*].InstanceId'
register: inst_id

Option 2

Upgrade Ansible to version 2.0 (Over the Hills and Far Away) and use the new ec2_remote_facts module

- ec2_remote_facts:
    filters:
      instance-state-name: stopped

Upvotes: 1

Bruce P
Bruce P

Reputation: 20759

You should add gather_facts: False to prevent Ansible from trying to SSH into the hosts since they're not running:

- 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.

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. 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. Using this you can get a list of the instances you're interested in and then start them up.

Upvotes: 0

Related Questions