build_image.yml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. - name: run the init
  124. import_playbook: ../../init/main.yml
  125. # This is the part that installs all of the software and configs for the instance
  126. # to become a node.
  127. - import_playbook: ../../openshift-node/private/image_prep.yml
  128. - name: Finish image preparation
  129. hosts: nodes
  130. become: yes
  131. tasks:
  132. - name: Update to latest package versions
  133. import_role:
  134. name: os_update_latest
  135. when:
  136. - ansible_distribution == "RedHat"
  137. - name: Unsubscribe image
  138. import_role:
  139. name: rhel_unsubscribe
  140. when:
  141. - ansible_distribution == "RedHat"
  142. - (rhsub_user is defined and rhsub_pass is defined) or (rhsub_ak is defined and rhsub_orgid is defined)
  143. - name: Commit image
  144. hosts: localhost
  145. connection: local
  146. tasks:
  147. - name: Stop the image VM
  148. os_server_action:
  149. server: "{{ image_vm.id }}"
  150. action: stop
  151. - name: Save the new image
  152. command: openstack server image create --wait --name "{{ openshift_openstack_default_image_name }}" "{{ image_vm.id }}"
  153. # Remove the temporary OpenStack resources
  154. - name: Remove the image build instance
  155. os_server:
  156. name: "{{ image_vm.id }}"
  157. state: absent
  158. - name: Remove the security group
  159. os_security_group:
  160. name: "{{ security_group.id }}"
  161. state: absent
  162. - name: Remove the router
  163. os_router:
  164. name: "{{ router.id }}"
  165. state: absent
  166. - name: Remove the subnet
  167. os_subnet:
  168. name: "{{ subnet.id }}"
  169. state: absent
  170. - name: Remove the network
  171. os_network:
  172. name: "{{ network.id }}"
  173. state: absent