grow_docker_vg.yml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ---
  2. # This playbook grows the docker VG on a node by:
  3. # * add a new volume
  4. # * add volume to the existing VG.
  5. # * pv move to the new volume.
  6. # * remove old volume
  7. # * detach volume
  8. # * mark old volume in AWS with "REMOVE ME" tag
  9. # * grow docker LVM to 90% of the VG
  10. #
  11. # To run:
  12. # 1. Source your AWS credentials (make sure it's the corresponding AWS account) into your environment
  13. # export AWS_ACCESS_KEY_ID='XXXXX'
  14. # export AWS_SECRET_ACCESS_KEY='XXXXXX'
  15. #
  16. # 2. run the playbook:
  17. # ansible-playbook -e 'cli_tag_name=<tag-name>' grow_docker_vg.yml
  18. #
  19. # Example:
  20. # ansible-playbook -e 'cli_tag_name=ops-compute-12345' grow_docker_vg.yml
  21. #
  22. # Notes:
  23. # * By default this will do a 55GB GP2 volume. The can be overidden with the "-e 'cli_volume_size=100'" variable
  24. # * This does a GP2 by default. Support for Provisioned IOPS has not been added
  25. # * This will assign the new volume to /dev/xvdc. This is not variablized, yet.
  26. # * This can be done with NO downtime on the host
  27. #
  28. - name: Grow the docker volume group
  29. hosts: "tag_Name_{{ cli_tag_name }}"
  30. user: root
  31. connection: ssh
  32. gather_facts: no
  33. vars:
  34. cli_volume_type: gp2
  35. cli_volume_size: 55
  36. # cli_volume_iops: "{{ 30 * cli_volume_size }}"
  37. pre_tasks:
  38. - fail:
  39. msg: "This playbook requires {{item}} to be set."
  40. when: "{{ item }} is not defined or {{ item }} == ''"
  41. with_items:
  42. - cli_tag_name
  43. - cli_volume_size
  44. - debug:
  45. var: hosts
  46. - name: start docker
  47. service:
  48. name: docker
  49. state: started
  50. - name: Determine if Storage Driver (docker info) is devicemapper
  51. shell: docker info | grep 'Storage Driver:.*devicemapper'
  52. register: device_mapper_check
  53. ignore_errors: yes
  54. - debug:
  55. var: device_mapper_check
  56. - name: fail if we don't detect devicemapper
  57. fail:
  58. msg: The "Storage Driver" in "docker info" is not set to "devicemapper"! Please investigate manually.
  59. when: device_mapper_check.rc == 1
  60. # docker-storage-setup creates a docker-pool as the lvm. I am using docker-pool lvm to test
  61. # and find the volume group.
  62. - name: Attempt to find the Volume Group that docker is using
  63. shell: lvs | grep docker-pool | awk '{print $2}'
  64. register: docker_vg_name
  65. ignore_errors: yes
  66. - debug:
  67. var: docker_vg_name
  68. - name: fail if we don't find a docker volume group
  69. fail:
  70. msg: Unable to find docker volume group. Please investigate manually.
  71. when: docker_vg_name.stdout_lines|length != 1
  72. # docker-storage-setup creates a docker-pool as the lvm. I am using docker-pool lvm to test
  73. # and find the physical volume.
  74. - name: Attempt to find the Phyisical Volume that docker is using
  75. shell: "pvs | grep {{ docker_vg_name.stdout }} | awk '{print $1}'"
  76. register: docker_pv_name
  77. ignore_errors: yes
  78. - debug:
  79. var: docker_pv_name
  80. - name: fail if we don't find a docker physical volume
  81. fail:
  82. msg: Unable to find docker physical volume. Please investigate manually.
  83. when: docker_pv_name.stdout_lines|length != 1
  84. - name: get list of volumes from AWS
  85. delegate_to: localhost
  86. ec2_vol:
  87. state: list
  88. instance: "{{ ec2_id }}"
  89. region: "{{ ec2_region }}"
  90. register: attached_volumes
  91. - debug: var=attached_volumes
  92. - name: get volume id of current docker volume
  93. set_fact:
  94. old_docker_volume_id: "{{ attached_volumes.volumes | translate_volume_name(docker_pv_name.stdout) }}"
  95. - debug: var=old_docker_volume_id
  96. - name: check to see if /dev/xvdc exists
  97. command: test -e /dev/xvdc
  98. register: xvdc_check
  99. ignore_errors: yes
  100. - debug: var=xvdc_check
  101. - name: fail if /dev/xvdc already exists
  102. fail:
  103. msg: /dev/xvdc already exists. Please investigate
  104. when: xvdc_check.rc == 0
  105. - name: Create a volume and attach it
  106. delegate_to: localhost
  107. ec2_vol:
  108. state: present
  109. instance: "{{ ec2_id }}"
  110. region: "{{ ec2_region }}"
  111. volume_size: "{{ cli_volume_size | default(30, True)}}"
  112. volume_type: "{{ cli_volume_type }}"
  113. device_name: /dev/xvdc
  114. register: create_volume
  115. - debug: var=create_volume
  116. - name: Fail when problems creating volumes and attaching
  117. fail:
  118. msg: "Failed to create or attach volume msg: {{ create_volume.msg }}"
  119. when: create_volume.msg is defined
  120. - name: tag the vol with a name
  121. delegate_to: localhost
  122. ec2_tag: region={{ ec2_region }} resource={{ create_volume.volume_id }}
  123. args:
  124. tags:
  125. Name: "{{ ec2_tag_Name }}"
  126. env: "{{ ec2_tag_environment }}"
  127. register: voltags
  128. - name: check for attached drive
  129. command: test -b /dev/xvdc
  130. register: attachment_check
  131. until: attachment_check.rc == 0
  132. retries: 30
  133. delay: 2
  134. - name: partition the new drive and make it lvm
  135. command: parted /dev/xvdc --script -- mklabel msdos mkpart primary 0% 100% set 1 lvm
  136. - name: pvcreate /dev/xvdc
  137. command: pvcreate /dev/xvdc1
  138. - name: Extend the docker volume group
  139. command: vgextend "{{ docker_vg_name.stdout }}" /dev/xvdc1
  140. - name: pvmove onto new volume
  141. command: "pvmove {{ docker_pv_name.stdout }} /dev/xvdc1"
  142. async: 3600
  143. poll: 10
  144. - name: Remove the old docker drive from the volume group
  145. command: "vgreduce {{ docker_vg_name.stdout }} {{ docker_pv_name.stdout }}"
  146. - name: Remove the pv from the old drive
  147. command: "pvremove {{ docker_pv_name.stdout }}"
  148. - name: Extend the docker lvm
  149. command: "lvextend -l '90%VG' /dev/{{ docker_vg_name.stdout }}/docker-pool"
  150. - name: detach old docker volume
  151. delegate_to: localhost
  152. ec2_vol:
  153. region: "{{ ec2_region }}"
  154. id: "{{ old_docker_volume_id }}"
  155. instance: None
  156. - name: tag the old vol valid label
  157. delegate_to: localhost
  158. ec2_tag: region={{ ec2_region }} resource={{old_docker_volume_id}}
  159. args:
  160. tags:
  161. Name: "{{ ec2_tag_Name }} REMOVE ME"
  162. register: voltags
  163. - name: Update the /etc/sysconfig/docker-storage-setup with new device
  164. lineinfile:
  165. dest: /etc/sysconfig/docker-storage-setup
  166. regexp: ^DEVS=
  167. line: DEVS=/dev/xvdc