create_pv.yaml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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: Configure the drive
  48. gather_facts: no
  49. hosts: oo_master
  50. user: root
  51. connection: ssh
  52. vars:
  53. pv_tmpdir: /tmp/persistentvolumes
  54. post_tasks:
  55. - name: Setting facts for template
  56. set_fact:
  57. pv_name: "pv-{{cli_volume_size}}-{{ hostvars[hostvars.localhost.oo_name]['ec2_tag_Name'] }}-{{hostvars.localhost.vol.volume_id }}"
  58. vol_az: "{{ hostvars[hostvars.localhost.oo_name]['ec2_placement'] }}"
  59. vol_id: "{{ hostvars.localhost.vol.volume_id }}"
  60. vol_size: "{{ cli_volume_size }}"
  61. pv_mntdir: "{{ pv_tmpdir }}/mnt-{{ 1000 | random }}"
  62. - set_fact:
  63. pv_template: "{{ pv_tmpdir }}/{{ pv_name }}.yaml"
  64. - name: "Mkdir {{ pv_tmpdir }}"
  65. file:
  66. state: directory
  67. path: "{{ pv_tmpdir }}"
  68. mode: '0750'
  69. - name: "Mkdir {{ pv_mntdir }}"
  70. file:
  71. state: directory
  72. path: "{{ pv_mntdir }}"
  73. mode: '0750'
  74. - name: Create pv file from template
  75. template:
  76. src: ./pv-template.j2
  77. dest: "{{ pv_template }}"
  78. owner: root
  79. mode: '0640'
  80. - name: mkfs
  81. filesystem:
  82. dev: "{{ cli_device_name }}"
  83. fstype: ext4
  84. - name: Mount the dev
  85. mount:
  86. name: "{{ pv_mntdir }}"
  87. src: "{{ cli_device_name }}"
  88. fstype: ext4
  89. state: mounted
  90. - name: chgrp g+rwXs
  91. file:
  92. path: "{{ pv_mntdir }}"
  93. mode: 'g+rwXs'
  94. recurse: yes
  95. seuser: system_u
  96. serole: object_r
  97. setype: svirt_sandbox_file_t
  98. selevel: s0
  99. - name: umount
  100. mount:
  101. name: "{{ pv_mntdir }}"
  102. src: "{{ cli_device_name }}"
  103. state: unmounted
  104. fstype: ext4
  105. - name: detach drive
  106. delegate_to: localhost
  107. ec2_vol:
  108. region: "{{ hostvars[hostvars.localhost.oo_name].ec2_region }}"
  109. id: "{{ hostvars.localhost.vol.volume_id }}"
  110. instance: None
  111. - name: "Remove {{ pv_mntdir }}"
  112. file:
  113. state: absent
  114. path: "{{ pv_mntdir }}"
  115. # We have to use the shell module because we can't set env vars with the command module.
  116. - name: "Place PV into oc"
  117. shell: "KUBECONFIG=/etc/openshift/master/admin.kubeconfig oc create -f {{ pv_template | quote }}"
  118. register: oc_output
  119. - debug: var=oc_output
  120. - fail:
  121. msg: "Failed to add {{ pv_template }} to master."
  122. when: oc_output.rc != 0