|
@@ -0,0 +1,134 @@
|
|
|
+---
|
|
|
+- name: Create a volume and attach it to master
|
|
|
+ hosts: localhost
|
|
|
+ gather_facts: no
|
|
|
+ vars:
|
|
|
+ cli_volume_type: gp2
|
|
|
+ cli_volume_iops: ''
|
|
|
+ oo_name: "{{ groups['tag_host-type_' ~ cli_hosttype] |
|
|
|
+ intersect(groups['tag_environment_' ~ cli_environment]) |
|
|
|
+ first }}"
|
|
|
+ pre_tasks:
|
|
|
+ - fail:
|
|
|
+ msg: "This playbook requires {{item}} to be set."
|
|
|
+ when: "{{ item }} is not defined or {{ item }} == ''"
|
|
|
+ with_items:
|
|
|
+ - cli_volume_size
|
|
|
+ - cli_device_name
|
|
|
+ - cli_hosttype
|
|
|
+ - cli_environment
|
|
|
+
|
|
|
+ - name: set oo_name fact
|
|
|
+ set_fact:
|
|
|
+ oo_name: "{{ oo_name }}"
|
|
|
+
|
|
|
+
|
|
|
+ - name: Select a single master to run this on
|
|
|
+ add_host:
|
|
|
+ hostname: "{{ oo_name }}"
|
|
|
+ ansible_ssh_host: "{{ hostvars[oo_name].ec2_public_dns_name }}"
|
|
|
+ groups: oo_master
|
|
|
+
|
|
|
+ - name: Create a volume and attach it
|
|
|
+ ec2_vol:
|
|
|
+ state: present
|
|
|
+ instance: "{{ hostvars[oo_name]['ec2_id'] }}"
|
|
|
+ region: "{{ hostvars[oo_name]['ec2_region'] }}"
|
|
|
+ volume_size: "{{ cli_volume_size }}"
|
|
|
+ volume_type: "{{ cli_volume_type }}"
|
|
|
+ device_name: "{{ cli_device_name }}"
|
|
|
+ iops: "{{ cli_volume_iops }}"
|
|
|
+ register: vol
|
|
|
+
|
|
|
+ - debug: var=vol
|
|
|
+
|
|
|
+- name: Configure the drive
|
|
|
+ gather_facts: no
|
|
|
+ hosts: oo_master
|
|
|
+ user: root
|
|
|
+ connection: ssh
|
|
|
+ vars:
|
|
|
+ pv_tmpdir: /tmp/persistentvolumes
|
|
|
+
|
|
|
+ post_tasks:
|
|
|
+ - name: Setting facts for template
|
|
|
+ set_fact:
|
|
|
+ pv_name: "pv-{{cli_volume_size}}-{{ hostvars[hostvars.localhost.oo_name]['ec2_tag_Name'] }}-{{hostvars.localhost.vol.volume_id }}"
|
|
|
+ vol_az: "{{ hostvars[hostvars.localhost.oo_name]['ec2_placement'] }}"
|
|
|
+ vol_id: "{{ hostvars.localhost.vol.volume_id }}"
|
|
|
+ vol_size: "{{ cli_volume_size }}"
|
|
|
+ pv_mntdir: "{{ pv_tmpdir }}/mnt-{{ 1000 | random }}"
|
|
|
+
|
|
|
+ - set_fact:
|
|
|
+ pv_template: "{{ pv_tmpdir }}/{{ pv_name }}.yaml"
|
|
|
+
|
|
|
+ - name: "Mkdir {{ pv_tmpdir }}"
|
|
|
+ file:
|
|
|
+ state: directory
|
|
|
+ path: "{{ pv_tmpdir }}"
|
|
|
+ mode: '0750'
|
|
|
+
|
|
|
+ - name: "Mkdir {{ pv_mntdir }}"
|
|
|
+ file:
|
|
|
+ state: directory
|
|
|
+ path: "{{ pv_mntdir }}"
|
|
|
+ mode: '0750'
|
|
|
+
|
|
|
+ - name: Create pv file from template
|
|
|
+ template:
|
|
|
+ src: ./pv-template.j2
|
|
|
+ dest: "{{ pv_template }}"
|
|
|
+ owner: root
|
|
|
+ mode: '0640'
|
|
|
+
|
|
|
+ - name: mkfs
|
|
|
+ filesystem:
|
|
|
+ dev: "{{ cli_device_name }}"
|
|
|
+ fstype: ext4
|
|
|
+
|
|
|
+ - name: Mount the dev
|
|
|
+ mount:
|
|
|
+ name: "{{ pv_mntdir }}"
|
|
|
+ src: "{{ cli_device_name }}"
|
|
|
+ fstype: ext4
|
|
|
+ state: mounted
|
|
|
+
|
|
|
+ - name: chgrp g+rwXs
|
|
|
+ file:
|
|
|
+ path: "{{ pv_mntdir }}"
|
|
|
+ mode: 'g+rwXs'
|
|
|
+ recurse: yes
|
|
|
+ seuser: system_u
|
|
|
+ serole: object_r
|
|
|
+ setype: svirt_sandbox_file_t
|
|
|
+ selevel: s0
|
|
|
+
|
|
|
+ - name: umount
|
|
|
+ mount:
|
|
|
+ name: "{{ pv_mntdir }}"
|
|
|
+ src: "{{ cli_device_name }}"
|
|
|
+ state: unmounted
|
|
|
+ fstype: ext4
|
|
|
+
|
|
|
+ - name: detach drive
|
|
|
+ delegate_to: localhost
|
|
|
+ ec2_vol:
|
|
|
+ region: "{{ hostvars[hostvars.localhost.oo_name].ec2_region }}"
|
|
|
+ id: "{{ hostvars.localhost.vol.volume_id }}"
|
|
|
+ instance: None
|
|
|
+
|
|
|
+ - name: "Remove {{ pv_mntdir }}"
|
|
|
+ file:
|
|
|
+ state: absent
|
|
|
+ path: "{{ pv_mntdir }}"
|
|
|
+
|
|
|
+ # We have to use the shell module because we can't set env vars with the command module.
|
|
|
+ - name: "Place PV into oc"
|
|
|
+ shell: "KUBECONFIG=/etc/openshift/master/admin.kubeconfig oc create -f {{ pv_template | quote }}"
|
|
|
+ register: oc_output
|
|
|
+
|
|
|
+ - debug: var=oc_output
|
|
|
+
|
|
|
+ - fail:
|
|
|
+ msg: "Failed to add {{ pv_template }} to master."
|
|
|
+ when: oc_output.rc != 0
|