From ebc1b4901ede2379ed51c0d7c62980600fbd0b1e Mon Sep 17 00:00:00 2001 From: abrown38 Date: Fri, 3 Jan 2025 12:41:39 +0000 Subject: [PATCH] New role to build new use vms --- ansible/playbooks/win_vms.yml | 9 ++++++ ansible/roles/win_vms/tasks/application.yml | 35 +++++++++++++++++++++ ansible/roles/win_vms/tasks/gpu.yml | 35 +++++++++++++++++++++ ansible/roles/win_vms/tasks/main.yml | 15 +++++++++ ansible/roles/win_vms/tasks/workbench.yml | 35 +++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 ansible/playbooks/win_vms.yml create mode 100644 ansible/roles/win_vms/tasks/application.yml create mode 100644 ansible/roles/win_vms/tasks/gpu.yml create mode 100644 ansible/roles/win_vms/tasks/main.yml create mode 100644 ansible/roles/win_vms/tasks/workbench.yml diff --git a/ansible/playbooks/win_vms.yml b/ansible/playbooks/win_vms.yml new file mode 100644 index 0000000..3336b63 --- /dev/null +++ b/ansible/playbooks/win_vms.yml @@ -0,0 +1,9 @@ +--- +- hosts: "localhost" + gather_facts: false + vars_files: + - ../vars/common.yml + - ../vars/{{ project_zone }}.yml + roles: + - role: win_vms + tags: win_vms diff --git a/ansible/roles/win_vms/tasks/application.yml b/ansible/roles/win_vms/tasks/application.yml new file mode 100644 index 0000000..7a9111f --- /dev/null +++ b/ansible/roles/win_vms/tasks/application.yml @@ -0,0 +1,35 @@ +--- +- name: Create application VMs (SEQUENTIAL) + os_server: + name: "{{ os_project }}-{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" + state: "{{ state }}" + image: "{{ vm_image }}" + flavor: "{{ user_vm_flavor }}" + timeout: 900 + boot_from_volume: true + volume_size: 40 + terminate_volume: yes + security_groups: + - domain_member + - user_vms + network: "{{ os_network }}" + metadata: + name: "app{{ '%02d' | format(item+1+app_quantity|int) }}" + win_project: "{{ win_project }}" + type: client + variant: application + os: windows + new_vm: "true" # Tag to target only new VMs + userdata: | + #ps1 + Rename-Computer "{{ win_project }}-app{{ '%02d' | format(item+1+app_quantity|int) }}" + $admin = [adsi]("WinNT://./Administrator, user") + $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") + Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + 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 }}" diff --git a/ansible/roles/win_vms/tasks/gpu.yml b/ansible/roles/win_vms/tasks/gpu.yml new file mode 100644 index 0000000..0382392 --- /dev/null +++ b/ansible/roles/win_vms/tasks/gpu.yml @@ -0,0 +1,35 @@ +--- +- name: Create gpu VMs (SEQUENTIAL) + os_server: + name: "{{ os_project }}-{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + state: "{{ state }}" + image: "{{ vm_image }}" + flavor: "{{ user_vm_flavor }}" + timeout: 900 + boot_from_volume: true + volume_size: 40 + terminate_volume: yes + security_groups: + - domain_member + - user_vms + network: "{{ os_network }}" + metadata: + name: "gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + win_project: "{{ win_project }}" + type: client + variant: gpu + os: windows + new_vm: "true" # Tag to target only new VMs + userdata: | + #ps1 + Rename-Computer "{{ win_project }}-gpu{{ '%02d' | format(item+1+gpu_quantity|int) }}" + $admin = [adsi]("WinNT://./Administrator, user") + $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") + Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + 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 }}" diff --git a/ansible/roles/win_vms/tasks/main.yml b/ansible/roles/win_vms/tasks/main.yml new file mode 100644 index 0000000..e40550a --- /dev/null +++ b/ansible/roles/win_vms/tasks/main.yml @@ -0,0 +1,15 @@ +--- +- import_tasks: workbench.yml + when: new_wb_quantity|int > 0 + +- import_tasks: application.yml + when: new_app_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 diff --git a/ansible/roles/win_vms/tasks/workbench.yml b/ansible/roles/win_vms/tasks/workbench.yml new file mode 100644 index 0000000..9e5f306 --- /dev/null +++ b/ansible/roles/win_vms/tasks/workbench.yml @@ -0,0 +1,35 @@ +--- +- name: Create workbench VMs (SEQUENTIAL) + os_server: + name: "{{ os_project }}-{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + state: "{{ state }}" + image: "{{ vm_image }}" + flavor: "{{ user_vm_flavor }}" + timeout: 900 + boot_from_volume: true + volume_size: 40 + terminate_volume: yes + security_groups: + - domain_member + - user_vms + network: "{{ os_network }}" + metadata: + name: "wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + win_project: "{{ win_project }}" + type: client + variant: workbench + os: windows + new_vm: "true" # Tag to target only new VMs + userdata: | + #ps1 + Rename-Computer "{{ win_project }}-wb{{ '%02d' | format(item+1+wb_quantity|int) }}" + $admin = [adsi]("WinNT://./Administrator, user") + $admin.PSBase.Invoke("SetPassword", "{{ vm_initial_password }}") + Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('{{ winrm_script_url }}')) + 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 }}"