Created
June 18, 2018 17:15
-
-
Save rsvalerio/4782fc0b0e15b7778e8675af94cd2348 to your computer and use it in GitHub Desktop.
disk and vm management in aws with ansible
This file contains hidden or 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/env ansible-playbook | |
- hosts: localhost | |
gather_facts: no | |
become: no | |
vars: | |
service: couchdb | |
region: us-east-1 | |
vm_name: disk-test-rodrigo-delete-me-please2 | |
volume_name: "volume_{{service}}" | |
device_name: /dev/xvdf | |
filesystem_type: xfs | |
mountpt: /data | |
tasks: | |
- name: create vm | |
ec2: | |
id: "{{vm_name}}" | |
instance_type: t2.micro | |
image: ami-1f178c60 | |
key_name: terraform | |
wait: yes | |
region: "{{region}}" | |
vpc_subnet_id: subnet-abcdef | |
instance_tags: | |
Name: "{{vm_name}}" | |
register: result_vm_created | |
tags: vm | |
- debug: var=result_vm_created | |
tags: vm | |
- name: save vm ip | |
set_fact: | |
vm_ip: "{{result_vm_created.instances[0].private_ip}}" | |
- debug: var=vm_ip | |
tags: vm | |
- name: create ebs volume | |
ec2_vol: | |
region: "{{region}}" | |
instance: "{{ result_vm_created.instance_ids[0] }}" | |
name: "{{volume_name}}" | |
volume_size: 5 | |
volume_type: gp2 | |
device_name: "{{device_name}}" | |
delete_on_termination: no | |
tags: | |
service: "{{volume_name}}" | |
register: result_volume_created | |
tags: | |
- volume | |
- debug: var=result_volume_created | |
- name: INVENTORY | update inventory in memory | |
add_host: | |
name: "{{vm_ip}}" | |
vm_name: "{{vm_name}}" | |
vm_ip: "{{vm_ip}}" | |
ansible_ssh_host: "{{vm_ip}}" | |
ansible_ssh_user: "ubuntu" | |
ansible_ssh_private_key_file: ~/.ssh/mykey_rsa | |
changed_when: False | |
- block: | |
- name: DISK | print info | |
debug: | |
msg: Preparing to format {{device_name}} on {{vm_name }} at {{ansible_host}} | |
- name: DISK | set fact | |
set_fact: | |
partition: "{{device_name}}1" | |
- name: DISK | wait for {{ device_name }} | |
wait_for: | |
path: "{{ device_name }}" | |
state: present | |
- name: DISK | partition {{ device_name }} | |
parted: | |
device: "{{ device_name }}" | |
number: 1 | |
state: present | |
register: partition_result | |
- name: DISK | print partition info | |
debug: | |
var: partition_result | |
verbosity: 1 | |
- name: DISK | format {{ partition }} with {{ filesystem_type }} | |
filesystem: | |
fstype: "{{ filesystem_type }}" | |
dev: "{{ partition }}" | |
- name: DISK | create mount point {{ mountpt }} | |
file: | |
path: "{{ mountpt }}" | |
state: directory | |
- name: DISK | mount {{ partition }} to {{ mountpt }} | |
mount: | |
src: "{{ partition }}" | |
name: "{{ mountpt }}" | |
fstype: "{{ filesystem_type }}" | |
#opts: "rw,relatime,data=ordered" | |
state: mounted | |
delegate_to: "{{vm_ip}}" | |
become: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment