Last active
March 29, 2016 19:12
-
-
Save szibis/cea9c351ef1d4922dc06 to your computer and use it in GitHub Desktop.
Ansible EC2 ephemeral custom facter to use in software RAID and with_items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import json | |
import backoff | |
import urllib2 | |
METADATA_URL_BASE = "http://169.254.169.254/2012-01-12/" | |
BLOCK_DEVICE_MAPPING = METADATA_URL_BASE + "meta-data/block-device-mapping/" | |
@backoff.on_exception(backoff.expo, | |
urllib2.URLError, | |
max_tries=3) | |
def get_http_data(resource_url): | |
response = urllib2.urlopen(resource_url, timeout = 2).read() | |
resp_list = [] | |
buff = [] | |
for item in response: | |
if item == '\n': | |
resp_list.append(''.join(buff)) | |
buff = [] | |
else: | |
buff.append(item) | |
else: | |
if buff: | |
resp_list.append(''.join(buff)) | |
return resp_list | |
bd_mapping = get_http_data(BLOCK_DEVICE_MAPPING) | |
dout = {} | |
deout = {} | |
rdout = {} | |
dout['ephemerals'] = {} | |
deout['ephemerals_external'] = {} | |
rdout['ephemerals_raid'] = {} | |
drive_list = [] | |
for eph in bd_mapping: | |
detailed_rul = METADATA_URL_BASE + "meta-data/block-device-mapping/" + eph | |
drive = get_http_data(detailed_rul) | |
drive_replaced = drive[0].replace('sd', 'xvd') | |
if "ephemeral" in eph: | |
drive_list.append(drive_replaced) | |
dout['ephemerals'][eph] = drive_replaced | |
deout['ephemerals_external'] = drive_list | |
drive_list = ["/dev/" + s for s in drive_list] | |
raid_drives = " ".join(drive_list) | |
rdout['ephemerals_raid'] = raid_drives | |
rdout['ephemerals_count'] = len(drive_list) | |
out_dict = dout.copy() | |
out_dict.update(deout) | |
out_dict.update(rdout) | |
print json.dumps(out_dict, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ephemerals_raid": "/dev/xvdb /dev/xvdc /dev/xvdd /dev/xvde /dev/xvdf /dev/xvdg /dev/xvdh /dev/xvdi", | |
"ephemerals_external": [ | |
"xvdb", | |
"xvdc", | |
"xvdd", | |
"xvde", | |
"xvdf", | |
"xvdg", | |
"xvdh", | |
"xvdi" | |
], | |
"ephemerals_count": 8, | |
"ephemerals": { | |
"ami": "/dev/sda1", | |
"ephemeral5": "xvdg", | |
"ephemeral4": "xvdf", | |
"ephemeral7": "xvdi", | |
"ephemeral6": "xvdh", | |
"ephemeral1": "xvdc", | |
"ephemeral0": "xvdb", | |
"ephemeral3": "xvde", | |
"ephemeral2": "xvdd", | |
"root": "/dev/sda1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ephemerals_raid": "", | |
"ephemerals_external": [], | |
"ephemerals_count": 0, | |
"ephemerals": { | |
"ami": "/dev/sda1", | |
"root": "/dev/sda1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ephemerals_raid": "/dev/sdb /dev/sdc", | |
"ephemerals_external": [ | |
"sdb", | |
"sdc" | |
], | |
"ephemerals_count": 2, | |
"ephemerals": { | |
"ami": "sda1", | |
"root": "/dev/sda1", | |
"ephemeral1": "sdc", | |
"ephemeral0": "sdb" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: build a new RAID0 array from local drives | |
shell: /bin/echo yes | /sbin/mdadm --create /dev/md0 --chunk=128 --level=0 --raid-devices={{ ansible_local.ephemeral.ephemerals_count }} {{ ansible_local.ephemeral.ephemerals_raid }} creates=/dev/md0 | |
when: ansible_local.ephemeral.ephemerals_count|int > 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment