create_pv.yaml 3.4 KB

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