create_machineset.yml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ---
  2. - name: Create machineset_name
  3. set_fact:
  4. machineset_name: "{{ item.metadata.name ~ '-rhel'}}"
  5. - name: Update machineset definition
  6. set_fact:
  7. machineset: "{{ item | combine(dict_edit, recursive=True) }}"
  8. vars:
  9. dict_edit:
  10. metadata:
  11. name: "{{ machineset_name }}"
  12. resourceVersion: ""
  13. spec:
  14. selector:
  15. matchLabels:
  16. machine.openshift.io/cluster-api-machineset: "{{ machineset_name }}"
  17. template:
  18. metadata:
  19. labels:
  20. machine.openshift.io/cluster-api-machineset: "{{ machineset_name }}"
  21. spec:
  22. providerSpec:
  23. value:
  24. ami:
  25. id: "{{ openshift_aws_scaleup_ami }}"
  26. keyName: "{{ openshift_aws_scaleup_key }}"
  27. - name: Import machineset definition
  28. command: >
  29. oc apply -f -
  30. --kubeconfig={{ kubeconfig_path }}
  31. register: oc_apply
  32. args:
  33. stdin: "{{ machineset | to_yaml }}"
  34. changed_when:
  35. - ('created' in oc_apply.stdout) or
  36. ('configured' in oc_apply.stdout)
  37. - block:
  38. - name: Get machines in the machineset
  39. command: >
  40. oc get machine
  41. --kubeconfig={{ kubeconfig_path }}
  42. --namespace=openshift-machine-api
  43. --selector='machine.openshift.io/cluster-api-machineset={{ machineset_name }}'
  44. --output=json
  45. register: oc_get_machine
  46. changed_when: false
  47. - name: Create list of machines
  48. set_fact:
  49. worker_machines: "{{ (oc_get_machine.stdout | from_json)['items'] | map(attribute='metadata.name') | list }}"
  50. - name: Wait for machines to be provisioned
  51. command: >
  52. oc get machine {{ item }}
  53. --kubeconfig={{ kubeconfig_path }}
  54. --namespace=openshift-machine-api
  55. --output=json
  56. loop: "{{ worker_machines }}"
  57. register: new_machine
  58. until:
  59. - new_machine.stdout != ''
  60. - (new_machine.stdout | from_json).status is defined
  61. - (new_machine.stdout | from_json).status.phase == 'Provisioned'
  62. retries: 36
  63. delay: 5
  64. changed_when: false
  65. - name: Get machines in the machineset after provisioning
  66. command: >
  67. oc get machine
  68. --kubeconfig={{ kubeconfig_path }}
  69. --namespace=openshift-machine-api
  70. --selector='machine.openshift.io/cluster-api-machineset={{ machineset_name }}'
  71. --output=json
  72. register: oc_get_machine
  73. changed_when: false
  74. - name: Add hostname to new_workers_list
  75. set_fact:
  76. new_workers_list: "{{ new_workers_list + [ item.status.addresses | selectattr('type', 'match', '^InternalDNS$') | map(attribute='address') | first ] }}"
  77. loop: "{{ (oc_get_machine.stdout | from_json)['items'] }}"
  78. rescue:
  79. - name: Machine creation failed
  80. fail:
  81. msg: "Machine creation failed, error: {{ new_machine.resources[0].status.errorMessage }}"
  82. - name: Get ssh bastion address
  83. command: >
  84. oc get service ssh-bastion
  85. --kubeconfig={{ kubeconfig_path }}
  86. --namespace=test-ssh-bastion
  87. --output=jsonpath='{.status.loadBalancer.ingress[0].hostname}'
  88. register: oc_get
  89. until:
  90. - oc_get.stdout != ''
  91. changed_when: false
  92. - name: Set fact ssh_bastion
  93. set_fact:
  94. ssh_bastion: "{{ oc_get.stdout }}"
  95. - name: Add machine to the inventory
  96. add_host:
  97. name: "{{ item }}"
  98. node_name: "{{ item }}"
  99. groups: new_workers
  100. ansible_ssh_common_args: "-o ProxyCommand=\"ssh -o IdentityFile='{{ openshift_aws_scaleup_key_path | default('/opt/app-root/src/.ssh/id_rsa') }}' -o ConnectTimeout=30 -o ConnectionAttempts=100 -o StrictHostKeyChecking=no -W %h:%p -q core@{{ ssh_bastion }}\""
  101. loop: "{{ new_workers_list }}"