diff --git a/ansible/playbooks/checkmk.yml b/ansible/playbooks/checkmk.yml index e564eb9..035472d 100644 --- a/ansible/playbooks/checkmk.yml +++ b/ansible/playbooks/checkmk.yml @@ -63,6 +63,11 @@ - name: Flush handlers meta: flush_handlers + - name: Pause for 30 seconds to allow changes to activate + ansible.builtin.pause: + seconds: 30 + run_once: true + - name: Create downtimes for new VMs block: - name: "Schedule downtime on Windows VMs to cover deployment" diff --git a/ansible/roles/win_vms/defaults/main.yml b/ansible/roles/win_vms/defaults/main.yml new file mode 100644 index 0000000..a51c8ba --- /dev/null +++ b/ansible/roles/win_vms/defaults/main.yml @@ -0,0 +1 @@ +cleanup: false \ No newline at end of file diff --git a/ansible/roles/win_vms/tasks/application.yml b/ansible/roles/win_vms/tasks/application.yml index 7a9111f..ea960b2 100644 --- a/ansible/roles/win_vms/tasks/application.yml +++ b/ansible/roles/win_vms/tasks/application.yml @@ -1,7 +1,9 @@ --- - name: Create application VMs (SEQUENTIAL) + vars: + vm_name: "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" os_server: - name: "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" + name: "{{ vm_name }}" state: "{{ state }}" image: "{{ vm_image }}" flavor: "{{ user_vm_flavor }}" @@ -26,10 +28,27 @@ $admin = [adsi]("WinNT://./Administrator, user") $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + when: + - "vm_name not in groups['meta-type_client']|default([])" + - cleanup == 'false' loop: "{{ range(new_app_quantity|int)|list }}" -- name: Increase application quantity variable - ansible.builtin.lineinfile: - path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml - regexp: '^app_quantity:\s*\d+' - line: "app_quantity: {{ app_quantity + new_app_quantity | int }}" +- name: cleanup + block: + - name: delete metadata from instance + vars: + vm_name: "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_app_quantity|int)|list }}" + + - name: Increase application quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^app_quantity:\s*\d+' + line: "app_quantity: {{ app_quantity + new_app_quantity | int }}" + when: cleanup == 'true' diff --git a/ansible/roles/win_vms/tasks/gpu.yml b/ansible/roles/win_vms/tasks/gpu.yml index 0382392..8b22d59 100644 --- a/ansible/roles/win_vms/tasks/gpu.yml +++ b/ansible/roles/win_vms/tasks/gpu.yml @@ -1,7 +1,9 @@ --- - name: Create gpu VMs (SEQUENTIAL) + vars: + vm_name: "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" os_server: - name: "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + name: "{{ vm_name }}" state: "{{ state }}" image: "{{ vm_image }}" flavor: "{{ user_vm_flavor }}" @@ -26,10 +28,27 @@ $admin = [adsi]("WinNT://./Administrator, user") $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + when: + - "vm_name not in groups['meta-type_client']|default([])" + - cleanup == 'false' loop: "{{ range(new_gpu_quantity|int)|list }}" -- name: Increase gpu quantity variable - ansible.builtin.lineinfile: - path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml - regexp: '^gpu_quantity:\s*\d+' - line: "gpu_quantity: {{ gpu_quantity + new_gpu_quantity | int }}" +- name: cleanup + block: + - name: delete metadata from instance + vars: + vm_name: "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_gpu_quantity|int)|list }}" + + - name: Increase gpu quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^gpu_quantity:\s*\d+' + line: "gpu_quantity: {{ gpu_quantity + new_gpu_quantity | int }}" + when: cleanup == 'true' diff --git a/ansible/roles/win_vms/tasks/main.yml b/ansible/roles/win_vms/tasks/main.yml index e40550a..63df216 100644 --- a/ansible/roles/win_vms/tasks/main.yml +++ b/ansible/roles/win_vms/tasks/main.yml @@ -1,15 +1,25 @@ --- -- import_tasks: workbench.yml - when: new_wb_quantity|int > 0 +- name: Create VMs + block: + - import_tasks: workbench.yml + when: new_wb_quantity|int > 0 -- import_tasks: application.yml - when: new_app_quantity|int > 0 + - import_tasks: application.yml + when: new_app_quantity|int > 0 -- import_tasks: gpu.yml - when: new_gpu_quantity|int > 0 + - import_tasks: gpu.yml + when: new_gpu_quantity|int > 0 -- name: Set Password Status - replace: - path: /deployments/{{ os_project }}-{{ win_project }}/PASSWORD - regexp: '^finished$' - replace: 'pending' \ No newline at end of file + - name: Set Password Status + replace: + path: /deployments/{{ os_project }}-{{ win_project }}/PASSWORD + regexp: '^finished$' + replace: 'pending' + when: + - new_wb_quantity|int > 0 + - new_app_quantity|int > 0 + - new_gpu_quantity|int > 0 + when: cleanup == 'false' + +- import_tasks: metadata.yml + when: cleanup == 'true' \ No newline at end of file diff --git a/ansible/roles/win_vms/tasks/metadata.yml b/ansible/roles/win_vms/tasks/metadata.yml new file mode 100644 index 0000000..e2ec490 --- /dev/null +++ b/ansible/roles/win_vms/tasks/metadata.yml @@ -0,0 +1,78 @@ +--- +- name: delete metadata from instance + vars: + vm_name: "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_wb_quantity|int)|list }}" + +- name: Increase workbench quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^wb_quantity:\s*\d+' + line: "wb_quantity: {{ wb_quantity + new_wb_quantity | int }}" + + +- name: delete metadata from application VMs + vars: + vm_name: "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_app_quantity|int)|list }}" + +- name: Increase workbench quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^wb_quantity:\s*\d+' + line: "wb_quantity: {{ app_quantity + new_app_quantity | int }}" + +- name: delete metadata from instance + vars: + vm_name: "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_gpu_quantity|int)|list }}" + +- name: Increase workbench quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^gpu_quantity:\s*\d+' + line: "wb_quantity: {{ gpu_quantity + new_gpu_quantity | int }}" + + +#- name: Remove temporary metadata +# tags: remove_metadata +# block: +# - name: Remove new metadata tag from GPU VMs +# shell: | +# export OS_CLIENT_CONFIG_FILE=/deployments/{{ os_project }}-{{ win_project }}/clouds.yaml +# export OS_CLOUD={{ os_project }} +# openstack server unset --property new_vm "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1|int) }}" +# loop: "{{ range(wb_quantity|int)|list }}" +# +# - name: Remove new metadata tag from GPU VMs +# shell: | +# export OS_CLIENT_CONFIG_FILE=/deployments/{{ os_project }}-{{ win_project }}/clouds.yaml +# export OS_CLOUD={{ os_project }} +# openstack server unset --property new_vm "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1|int) }}" +# loop: "{{ range(app_quantity|int)|list }}" +# +# - name: Remove new metadata tag from GPU VMs +# shell: | +# export OS_CLIENT_CONFIG_FILE=/deployments/{{ os_project }}-{{ win_project }}/clouds.yaml +# export OS_CLOUD={{ os_project }} +# openstack server unset --property new_vm "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1|int) }}" +# loop: "{{ range(gpu_quantity|int)|list }}" + diff --git a/ansible/roles/win_vms/tasks/workbench.yml b/ansible/roles/win_vms/tasks/workbench.yml index 9e5f306..14f3f1f 100644 --- a/ansible/roles/win_vms/tasks/workbench.yml +++ b/ansible/roles/win_vms/tasks/workbench.yml @@ -1,7 +1,9 @@ --- - name: Create workbench VMs (SEQUENTIAL) + vars: + vm_name: "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" os_server: - name: "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + name: "{{ vm_name }}" state: "{{ state }}" image: "{{ vm_image }}" flavor: "{{ user_vm_flavor }}" @@ -26,10 +28,27 @@ $admin = [adsi]("WinNT://./Administrator, user") $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + when: + - "vm_name not in groups['meta-type_client']|default([])" + - cleanup == 'false' loop: "{{ range(new_wb_quantity|int)|list }}" -- name: Increase workbench quantity variable - ansible.builtin.lineinfile: - path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml - regexp: '^wb_quantity:\s*\d+' - line: "wb_quantity: {{ wb_quantity + new_wb_quantity | int }}" +- name: cleanup + block: + - name: delete metadata from instance + vars: + vm_name: "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + openstack.cloud.server_metadata: + state: absent + cloud: "{{ os_project }}" + name: "{{ vm_name }}" + meta: + new_vm: + loop: "{{ range(new_wb_quantity|int)|list }}" + + - name: Increase workbench quantity variable + ansible.builtin.lineinfile: + path: /deployments/{{ os_project }}-{{ win_project }}/ansible/vars/common.yml + regexp: '^wb_quantity:\s*\d+' + line: "wb_quantity: {{ wb_quantity + new_wb_quantity | int }}" + when: cleanup == 'true'