launch_instances.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ---
  2. # TODO: Add support for choosing base image based on deployment_type and os
  3. # wanted (os wanted needs support added in bin/cluster with sane defaults:
  4. # fedora/centos for origin, rhel for online/enterprise)
  5. # TODO: create a role to encapsulate some of this complexity, possibly also
  6. # create a module to manage the storage tasks, network tasks, and possibly
  7. # even handle the libvirt tasks to set metadata in the domain xml and be able
  8. # to create/query data about vms without having to use xml the python libvirt
  9. # bindings look like a good candidate for this
  10. - name: Download Base Cloud image
  11. get_url:
  12. url: '{{ image_url }}'
  13. sha256sum: '{{ image_sha256 }}'
  14. dest: '{{ os_libvirt_storage_pool_path }}/{{ image_name }}'
  15. - name: Create the cloud-init config drive path
  16. file:
  17. dest: '{{ os_libvirt_storage_pool_path }}/{{ item }}_configdrive/'
  18. state: directory
  19. with_items: instances
  20. - name: Create the cloud-init config drive files
  21. template:
  22. src: '{{ item[1] }}'
  23. dest: '{{ os_libvirt_storage_pool_path }}/{{ item[0] }}_configdrive/{{ item[1] }}'
  24. with_nested:
  25. - instances
  26. - [ user-data, meta-data ]
  27. - name: Create the cloud-init config drive
  28. command: 'genisoimage -output {{ os_libvirt_storage_pool_path }}/{{ item }}_cloud-init.iso -volid cidata -joliet -rock user-data meta-data'
  29. args:
  30. chdir: '{{ os_libvirt_storage_pool_path }}/{{ item }}_configdrive/'
  31. creates: '{{ os_libvirt_storage_pool_path }}/{{ item }}_cloud-init.iso'
  32. with_items: instances
  33. - name: Refresh the libvirt storage pool for openshift
  34. command: 'virsh -c {{ libvirt_uri }} pool-refresh {{ libvirt_storage_pool }}'
  35. - name: Create VMs drives
  36. command: 'virsh -c {{ libvirt_uri }} vol-create-as {{ os_libvirt_storage_pool }} {{ item }}.qcow2 10G --format qcow2 --backing-vol {{ image_name }} --backing-vol-format qcow2'
  37. with_items: instances
  38. - name: Create VMs
  39. virt:
  40. name: '{{ item }}'
  41. command: define
  42. xml: "{{ lookup('template', '../templates/domain.xml') }}"
  43. uri: '{{ libvirt_uri }}'
  44. with_items: instances
  45. - name: Start VMs
  46. virt:
  47. name: '{{ item }}'
  48. state: running
  49. uri: '{{ libvirt_uri }}'
  50. with_items: instances
  51. - name: Collect MAC addresses of the VMs
  52. shell: 'virsh -c {{ libvirt_uri }} dumpxml {{ item }} | xmllint --xpath "string(//domain/devices/interface/mac/@address)" -'
  53. register: scratch_mac
  54. with_items: instances
  55. - name: Wait for the VMs to get an IP
  56. command: "egrep -c '{{ scratch_mac.results | oo_collect('stdout') | join('|') }}' /proc/net/arp"
  57. ignore_errors: yes
  58. register: nb_allocated_ips
  59. until: nb_allocated_ips.stdout == '{{ instances | length }}'
  60. retries: 30
  61. delay: 1
  62. - name: Collect IP addresses of the VMs
  63. shell: "awk '/{{ item.stdout }}/ {print $1}' /proc/net/arp"
  64. register: scratch_ip
  65. with_items: scratch_mac.results
  66. - set_fact:
  67. ips: "{{ scratch_ip.results | oo_collect('stdout') }}"
  68. - name: Add new instances
  69. add_host:
  70. hostname: '{{ item.0 }}'
  71. ansible_ssh_host: '{{ item.1 }}'
  72. ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}"
  73. ansible_sudo: "{{ deployment_vars[deployment_type].sudo }}"
  74. groups: 'tag_env-{{ cluster }}, tag_host-type-{{ type }}, tag_env-host-type-{{ cluster }}-openshift-{{ type }}'
  75. with_together:
  76. - instances
  77. - ips
  78. - name: Wait for ssh
  79. wait_for:
  80. host: '{{ item }}'
  81. port: 22
  82. with_items: ips
  83. - name: Wait for openshift user setup
  84. command: 'ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null openshift@{{ item.1 }} echo openshift user is setup'
  85. register: result
  86. until: result.rc == 0
  87. retries: 30
  88. delay: 1
  89. with_together:
  90. - instances
  91. - ips