Skip to content

Instantly share code, notes, and snippets.

@pavgup
Created November 24, 2015 07:28
Show Gist options
  • Save pavgup/4c4b31f006247e3e9fbd to your computer and use it in GitHub Desktop.
Save pavgup/4c4b31f006247e3e9fbd to your computer and use it in GitHub Desktop.
Surprisingly annoying use of conditionals and variables with ec2 instances and volumes. The ec2 module can comfortably create volumes that have controlled delete_on_termination flags but you must first provision the instance and the volume before you are able to tag the volume. Assuming you wanted to tag a single volume based on its device_name …
---
data_volume:
- name: mysql-data-drive
device_name: /dev/sdb
device_type: gp2
volume_size: 500
delete_on_termination: false
---
vars_files:
- vars/secure-aws.yml
- vars/default-aws.yml
- vars/mysql.yml
- name: Launch mysql instance
ec2:
region: "{{ ec2_region }}"
ec2_access_key: "{{ ec2_access_key }}"
ec2_secret_key: "{{ ec2_secret_key }}"
keypair: "{{ ec2_keypair }}"
instance_type: t2.large
image: "{{ ec2_image }}"
instance_tags:
Name: mysql
Hosting: mysql-db
Application: mysql-db
Hostname: mysql-db
Expiry: november-2016
Owner: pavgup
volumes: "{{ data_volume }}"
exact_count: 1
count_tag:
Name: mysql
group: ['default', 'mysql-ssh-inside-vpc', 'zabbix-sg']
wait: true
register: ec2_mysql
- debug: var=ec2_mysql
- name: grab the just-created volume information for tagging
ec2_vol:
instance: "{{ item.id }}"
region: "{{ ec2_region }}"
state: list
with_items: ec2_mysql.tagged_instances
register: ec2_vol
- debug: var=ec2_vol.results[0].volumes[0]
- debug: var=data_volume
- debug: var=data_volume[0].device_name
- name: tag the volumes that match the data volume size
ec2_tag:
resource: "{{ item.1.id }}"
region: "{{ ec2_region }}"
state: present
tags:
Name: "{{ data_volume[0].name }}"
with_subelements:
- ec2_vol.results
- volumes
when: '"{{item.1.attachment_set.device}}" == "{{data_volume[0].device_name}}"'
@rem-aj
Copy link

rem-aj commented Dec 5, 2016

This was great, thanks so much. the examples on ansible doc were missing some crucial information. For anyone who has multiple volumes, remove the conditional statement and you should be set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment