create_pv.yaml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ---
  2. #example run:
  3. # ansible-playbook -e "cli_volume_size=1" \
  4. # -e "cli_device_name=/dev/xvdf" \
  5. # -e "cli_hosttype=master" \
  6. # -e "cli_environment=ops" \
  7. # create_pv.yaml
  8. # FIXME: we need to change "environment" to "clusterid" as that's what it really is now.
  9. #
  10. - name: Create a volume and attach it to master
  11. hosts: localhost
  12. gather_facts: no
  13. vars:
  14. cli_volume_type: gp2
  15. cli_volume_iops: ''
  16. oo_name: "{{ groups['tag_host-type_' ~ cli_hosttype] |
  17. intersect(groups['tag_environment_' ~ cli_environment]) |
  18. first }}"
  19. pre_tasks:
  20. - fail:
  21. msg: "This playbook requires {{item}} to be set."
  22. when: "{{ item }} is not defined or {{ item }} == ''"
  23. with_items:
  24. - cli_volume_size
  25. - cli_device_name
  26. - cli_hosttype
  27. - cli_environment
  28. - name: set oo_name fact
  29. set_fact:
  30. oo_name: "{{ oo_name }}"
  31. - name: Select a single master to run this on
  32. add_host:
  33. hostname: "{{ oo_name }}"
  34. ansible_ssh_host: "{{ hostvars[oo_name].ec2_public_dns_name }}"
  35. groups: oo_master
  36. - name: Create a volume and attach it
  37. ec2_vol:
  38. state: present
  39. instance: "{{ hostvars[oo_name]['ec2_id'] }}"
  40. region: "{{ hostvars[oo_name]['ec2_region'] }}"
  41. volume_size: "{{ cli_volume_size }}"
  42. volume_type: "{{ cli_volume_type }}"
  43. device_name: "{{ cli_device_name }}"
  44. iops: "{{ cli_volume_iops }}"
  45. register: vol
  46. - debug: var=vol
  47. - name: tag the vol with a name
  48. ec2_tag: region={{ hostvars[oo_name]['ec2_region'] }} resource={{vol.volume_id}}
  49. args:
  50. tags:
  51. Name: "pv-{{ hostvars[oo_name]['ec2_tag_Name'] }}"
  52. env: "{{cli_environment}}"
  53. register: voltags
  54. - debug: var=voltags
  55. - name: Configure the drive
  56. gather_facts: no
  57. hosts: oo_master
  58. user: root
  59. connection: ssh
  60. vars:
  61. pv_tmpdir: /tmp/persistentvolumes
  62. post_tasks:
  63. - name: Setting facts for template
  64. set_fact:
  65. pv_name: "pv-{{cli_volume_size}}-{{ hostvars[hostvars.localhost.oo_name]['ec2_tag_Name'] }}-{{hostvars.localhost.vol.volume_id }}"
  66. vol_az: "{{ hostvars[hostvars.localhost.oo_name]['ec2_placement'] }}"
  67. vol_id: "{{ hostvars.localhost.vol.volume_id }}"
  68. vol_size: "{{ cli_volume_size }}"
  69. pv_mntdir: "{{ pv_tmpdir }}/mnt-{{ 1000 | random }}"
  70. - set_fact:
  71. pv_template: "{{ pv_tmpdir }}/{{ pv_name }}.yaml"
  72. - name: "Mkdir {{ pv_tmpdir }}"
  73. file:
  74. state: directory
  75. path: "{{ pv_tmpdir }}"
  76. mode: '0750'
  77. - name: "Mkdir {{ pv_mntdir }}"
  78. file:
  79. state: directory
  80. path: "{{ pv_mntdir }}"
  81. mode: '0750'
  82. - name: Create pv file from template
  83. template:
  84. src: ./pv-template.j2
  85. dest: "{{ pv_template }}"
  86. owner: root
  87. mode: '0640'
  88. - name: mkfs
  89. filesystem:
  90. dev: "{{ cli_device_name }}"
  91. fstype: ext4
  92. - name: Mount the dev
  93. mount:
  94. name: "{{ pv_mntdir }}"
  95. src: "{{ cli_device_name }}"
  96. fstype: ext4
  97. state: mounted
  98. - name: chgrp g+rwXs
  99. file:
  100. path: "{{ pv_mntdir }}"
  101. mode: 'g+rwXs'
  102. recurse: yes
  103. seuser: system_u
  104. serole: object_r
  105. setype: svirt_sandbox_file_t
  106. selevel: s0
  107. - name: umount
  108. mount:
  109. name: "{{ pv_mntdir }}"
  110. src: "{{ cli_device_name }}"
  111. state: unmounted
  112. fstype: ext4
  113. - name: remove from fstab
  114. mount:
  115. name: "{{ pv_mntdir }}"
  116. src: "{{ cli_device_name }}"
  117. state: absent
  118. fstype: ext4
  119. - name: detach drive
  120. delegate_to: localhost
  121. ec2_vol:
  122. region: "{{ hostvars[hostvars.localhost.oo_name].ec2_region }}"
  123. id: "{{ hostvars.localhost.vol.volume_id }}"
  124. instance: None
  125. - name: "Remove {{ pv_mntdir }}"
  126. file:
  127. state: absent
  128. path: "{{ pv_mntdir }}"
  129. # We have to use the shell module because we can't set env vars with the command module.
  130. - name: "Place PV into oc"
  131. shell: "KUBECONFIG=/etc/openshift/master/admin.kubeconfig oc create -f {{ pv_template | quote }}"
  132. register: oc_output
  133. - debug: var=oc_output
  134. - fail:
  135. msg: "Failed to add {{ pv_template }} to master."
  136. when: oc_output.rc != 0