build_image.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ---
  2. - name: Verify image build prerequisites
  3. hosts: localhost
  4. connection: local
  5. gather_facts: no
  6. tasks:
  7. - name: Require openshift_openstack_build_base_image
  8. fail:
  9. msg: "A base image name is required for image building. Please ensure `openshift_openstack_build_base_image` is defined."
  10. when: openshift_openstack_build_base_image is undefined
  11. - name: Require openshift_openstack_default_image_name
  12. fail:
  13. msg: >
  14. You must specify the name the new image will be saved to Glance as.
  15. Please make sure `openshift_openstack_default_image_name` is defined.
  16. when:
  17. - openshift_openstack_default_image_name is not defined
  18. - name: Get the target image facts
  19. os_image_facts:
  20. image: "{{ openshift_openstack_default_image_name }}"
  21. register: image_check
  22. - name: Verify that the resulting image does not exist
  23. fail:
  24. msg: >
  25. The image "{{ openshift_openstack_default_image_name }}" specified in
  26. `openshift_openstack_default_image_name` exists already.
  27. Please choose a different name or delete it.
  28. when: image_check.ansible_facts.openstack_image
  29. - set_fact:
  30. build_prefix: "{{ openshift_openstack_clusterid|default('build') }}.{{ openshift_openstack_public_dns_domain|default('example.com') }}-build"
  31. - name: Launch image build instance
  32. hosts: localhost
  33. connection: local
  34. gather_facts: yes
  35. tasks:
  36. # NOTE: we create a temporary network, subnet, router and security groups
  37. # to have a known environment to launch the image build VM in.
  38. # They get deleted once we save the image.
  39. # TODO(shadower): allow specifying an existing subnet etc. instead.
  40. - name: Create a network
  41. os_network:
  42. name: "{{ build_prefix }}-network"
  43. register: network
  44. - name: Create a subnet
  45. os_subnet:
  46. name: "{{ build_prefix }}-subnet"
  47. network_name: "{{ network.network.name }}"
  48. cidr: "{{ openshift_openstack_build_network_cidr | default('192.168.23.0/24') }}"
  49. dns_nameservers: "{{ openshift_openstack_dns_nameservers }}"
  50. register: subnet
  51. - name: Create the router
  52. os_router:
  53. name: "{{ build_prefix }}-router"
  54. network: "{{ openshift_openstack_external_network_name }}"
  55. interfaces:
  56. - "{{ subnet.id }}"
  57. register: router
  58. - name: Create a security group
  59. os_security_group:
  60. name: "{{ build_prefix }}-security-group"
  61. description: Security group for the image build server
  62. register: security_group
  63. - name: Allow pinging the server
  64. os_security_group_rule:
  65. security_group: "{{ security_group.id }}"
  66. protocol: icmp
  67. port_range_min: -1
  68. port_range_max: -1
  69. - name: Allow SSH access
  70. os_security_group_rule:
  71. security_group: "{{ security_group.id }}"
  72. protocol: tcp
  73. port_range_min: 22
  74. port_range_max: 22
  75. - name: Launch the image build instance
  76. os_server:
  77. name: "{{ build_prefix }}-image-server"
  78. network: "{{ network.id }}"
  79. auto_ip: yes
  80. flavor: "{{ openshift_openstack_default_flavor }}"
  81. image: "{{ openshift_openstack_build_base_image }}"
  82. key_name: "{{ openshift_openstack_keypair_name }}"
  83. security_groups:
  84. - "{{ security_group.id }}"
  85. # Create a known SSH user so we can log in to the VM.
  86. # TODO(shadower): should we create a temporary keypair & user here
  87. # and delete it when done?
  88. userdata: |
  89. #cloud-config
  90. system_info:
  91. default_user:
  92. name: {{ ansible_user|default('openshift') }}
  93. sudo: ["ALL=(ALL) NOPASSWD: ALL"]
  94. write_files:
  95. - path: /etc/sudoers.d/00-openshift-no-requiretty
  96. permissions: 440
  97. content: |
  98. Defaults:{{ ansible_user|default('openshift') }} !requiretty
  99. state: present
  100. register: image_vm
  101. - name: Add host to nodes
  102. add_host:
  103. name: "{{ image_vm.openstack.accessIPv4 }}"
  104. groups: nodes,OSEv3
  105. ansible_become: true
  106. - name: Wait for instance to respond to SSH
  107. wait_for:
  108. delay: 1
  109. host: "{{ image_vm.openstack.accessIPv4 }}"
  110. port: 22
  111. state: started
  112. timeout: 120
  113. - name: Wait for full SSH connection
  114. hosts: nodes
  115. gather_facts: no
  116. tasks:
  117. - wait_for_connection:
  118. - setup:
  119. - set_fact:
  120. openshift_node_image_prep_packages:
  121. - cloud-init
  122. - cloud-utils-growpart
  123. # This is the part that installs all of the software and configs for the instance
  124. # to become a node.
  125. - import_playbook: ../../openshift-node/private/image_prep.yml
  126. - name: Finish image preparation
  127. hosts: nodes
  128. become: yes
  129. tasks:
  130. - name: Update to latest package versions
  131. import_role:
  132. name: os_update_latest
  133. when:
  134. - ansible_distribution == "RedHat"
  135. - name: Unsubscribe image
  136. import_role:
  137. name: rhel_unsubscribe
  138. when:
  139. - ansible_distribution == "RedHat"
  140. - (rhsub_user is defined and rhsub_pass is defined) or (rhsub_ak is defined and rhsub_orgid is defined)
  141. - name: Commit image
  142. hosts: localhost
  143. connection: local
  144. tasks:
  145. - name: Stop the image VM
  146. os_server_action:
  147. server: "{{ image_vm.id }}"
  148. action: stop
  149. - name: Save the new image
  150. command: openstack server image create --wait --name "{{ openshift_openstack_default_image_name }}" "{{ image_vm.id }}"
  151. # Remove the temporary OpenStack resources
  152. - name: Remove the image build instance
  153. os_server:
  154. name: "{{ image_vm.id }}"
  155. state: absent
  156. - name: Remove the security group
  157. os_security_group:
  158. name: "{{ security_group.id }}"
  159. state: absent
  160. - name: Remove the router
  161. os_router:
  162. name: "{{ router.id }}"
  163. state: absent
  164. - name: Remove the subnet
  165. os_subnet:
  166. name: "{{ subnet.id }}"
  167. state: absent
  168. - name: Remove the network
  169. os_network:
  170. name: "{{ network.id }}"
  171. state: absent