Hi guys
I have a playbook that:

   - Creates an EC2 instance
   - Provisions the instance using a few roles
   - Creates an AMI out of that instance
   - Destroys the instance after the AMI is created successfully

Now the problem is if there's a failure somewhere after the instance 
creation, let's say npm is having issues which is quite normal or some 
ubuntu repository is having issues, and I run the playbook again, it will 
spin up a new instance. How can I handle this? Preferably a way to use the 
same instance if the failure happens before AMI creation part or at least 
automatically delete the instance if I hit an error before exiting the 
playbook successfully.

Here's my current playbook:

---
- name: Making preprations for the provisioning
  hosts: localhost
  connection: local
  gather_facts: no
  roles:
    - workstation

- name: Launch an Ubuntu 14.04 EC2 instance
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  - name: Find the latest Ubuntu AMI
    ec2_ami_search: distro=ubuntu release=trusty region=ap-southeast-1 
store=ebs-ssd virt=hvm
    register: ubuntu_image
  - name: Start the new EC2 instance
    ec2:
      image: "{{ ubuntu_image.ami }}"
      region: ap-southeast-1
      zone: ap-southeast-1b
      instance_type: t2.small
      vpc_subnet_id: subnet-xxxxxxxxx
      group_id: ['sg-xxxxxxxx', 'sg-xxxxxxxx']
      key_name: blahblah-deploy
      wait: yes
      wait_timeout: 500
      instance_tags:
        Name: basebox.exmaple.net
        Role: Blahblah Base AMI
    register: ec2
  - name: Add the new instance to host group
    add_host: hostname={{ item.private_ip }} groupname=launched
    with_items: ec2.instances
  - name: Wait for SSH to come up on the new instance
    wait_for: host={{ item.private_ip }} port=22 delay=60 timeout=320 
state=started
    with_items: ec2.instances

- name: Copy the code to the new instance
  hosts: launched
  user: ubuntu
  sudo: true
  tasks:
  - synchronize: src=code/ dest=/tmp/code delete=yes archive=yes

- name: Provision the new instance to create the base AMI
  hosts: launched
  user: ubuntu
  sudo: true
  vars:
    app_user: blahblah
    app_root: /home/{{ app_user }}/apps
  roles:
    - nodesource.node
    - basebox

- name: Remove the code from the instance before creating the AMI
  hosts: launched
  user: ubuntu
  sudo: true
  tasks:
  - file: path=/tmp/code state=absent

- name: Create an AMI from the provisioned instance
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  - ec2_ami: region=ap-southeast-1 instance_id={{ item.id }} wait=no 
name=basebox-2000000 wait=yes wait_timeout=300
    with_items:
      - "{{ ec2.instances }}"

- name: Delete the instance now that the AMI is created
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  - ec2:
      region: ap-southeast-1
      instance_ids: "{{ ec2.instance_ids }}"
      state: 'absent'

I'm not looking for the complete answer. Just a clue how to do it.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e7c898e5-69af-4f77-bb35-8586d8db2b13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to