From c8a6c4f60ed36e6d29461599695baa779aff4948 Mon Sep 17 00:00:00 2001 From: heqnx Date: Sat, 12 Jul 2025 12:00:33 +0300 Subject: adding a linux domain joined server --- ansible/group_vars/all/main.yaml | 3 ++ ansible/main.yaml | 54 +++++++++++++++++----- ansible/roles/proxmox_vm/tasks/set_network.yaml | 52 ++++++++++++++++++++- ansible/roles/proxmox_vm/tasks/upload_files.yaml | 1 + ansible/roles/srv01/tasks/join_domain.yaml | 4 ++ ansible/roles/srv01/tasks/main.yaml | 14 ++++++ ansible/roles/srv01/tasks/reboot.yaml | 5 ++ ansible/roles/srv01/tasks/set_hostname.yaml | 5 ++ .../srv01/templates/static_ip_netplan.yaml.j2 | 10 ++++ 9 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 ansible/roles/srv01/tasks/join_domain.yaml create mode 100644 ansible/roles/srv01/tasks/main.yaml create mode 100644 ansible/roles/srv01/tasks/reboot.yaml create mode 100644 ansible/roles/srv01/tasks/set_hostname.yaml create mode 100644 ansible/roles/srv01/templates/static_ip_netplan.yaml.j2 diff --git a/ansible/group_vars/all/main.yaml b/ansible/group_vars/all/main.yaml index 280d0d3..2a2246c 100644 --- a/ansible/group_vars/all/main.yaml +++ b/ansible/group_vars/all/main.yaml @@ -13,6 +13,9 @@ default_win_svc_password : "{{ lookup('ansible.builtin.env', 'windows_svc default_linux_username : "{{ lookup('ansible.builtin.env', 'linux_username') }}" default_linux_password : "{{ lookup('ansible.builtin.env', 'linux_password') }}" +windows_server_qcow_image : "{{ lookup('ansible.builtin.env', 'windows_server_qcow_image') }}" +linux_server_qcow_image : "{{ lookup('ansible.builtin.env', 'linux_server_qcow_image') }}" + windows_server_template_id : "{{ lookup('ansible.builtin.env', 'windows_server_template_id') }}" windows_server_template_name : "{{ lookup('ansible.builtin.env', 'windows_server_template_name') }}" windows_desktop_template_id : "{{ lookup('ansible.builtin.env', 'windows_desktop_template_id') }}" diff --git a/ansible/main.yaml b/ansible/main.yaml index cb46c17..777db8f 100644 --- a/ansible/main.yaml +++ b/ansible/main.yaml @@ -2,10 +2,11 @@ hosts: localhost gather_facts: no tasks: - - name: deploy main domain vm on proxmox + - name: deploy main domain controller vm on proxmox include_role: name: proxmox_vm vars: + os_type: "windows" template: "{{ windows_server_template_name }}" id: "{{ windows_server_template_id }}" vm: "{{ main_dc01_hostname }}" @@ -17,8 +18,8 @@ hostname: "{{ main_dc01_hostname }}" domain: "{{ main_domain_name }}" fqdn: "{{ main_dc01_hostname }}.{{ main_domain_name }}" - - - name: add windows vm to in-memory inventory + + - name: add domain controller vm to in-memory inventory add_host: name: "{{ main_dc01_hostname }}.{{ main_domain_name }}" ansible_host: "{{ main_dc01_ip_address }}" @@ -29,17 +30,46 @@ ansible_winrm_transport: basic ansible_winrm_server_cert_validation: ignore changed_when: false - -- name: configure windows domain controller - hosts: "{{ main_dc01_hostname }}.{{ main_domain_name }}" - gather_facts: no - vars_files: - - group_vars/all/connectors.yaml - - group_vars/all/main.yaml - tasks: - - name: configure windows dc + + - name: configure windows domain controller include_role: name: dc01 vars: hostname: "{{ main_dc01_hostname }}" domain_name: "{{ main_domain_name }}" + + - name: deploy linux domain vm on proxmox + include_role: + name: proxmox_vm + vars: + os_type: "linux" + template: "{{ linux_server_template_name }}" + id: "{{ linux_server_template_id }}" + vm: "{{ main_linux_srv01_hostname }}" + newid: "{{ main_linux_srv01_vmid }}" + vmid: "{{ main_linux_srv01_vmid }}" + ip: "{{ main_linux_srv01_ip_address }}" + gateway: "{{ network_gateway }}" + #dns: "{{ main_dc01_ip_address }}" + dns: "8.8.8.8" + hostname: "{{ main_linux_srv01_hostname }}" + domain: "{{ main_domain_name }}" + fqdn: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }}" + + - name: add linux domain vm to in-memory inventory + add_host: + name: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }}" + ansible_host: "{{ main_linux_srv01_ip_address }}" + ansible_connection: "{{ linux_connector }}" + ansible_user: "{{ default_linux_username }}" + ansible_password: "{{ default_linux_password }}" + ansible_port: "{{ linux_port }}" + ansible_host_key_checking: false + changed_when: false + + - name: configure linux domain vm + include_role: + name: srv01 + vars: + hostname: "{{ main_linux_srv01_hostname }}" + domain_name: "{{ main_domain_name }}" diff --git a/ansible/roles/proxmox_vm/tasks/set_network.yaml b/ansible/roles/proxmox_vm/tasks/set_network.yaml index 20ab31c..fb088ad 100644 --- a/ansible/roles/proxmox_vm/tasks/set_network.yaml +++ b/ansible/roles/proxmox_vm/tasks/set_network.yaml @@ -1,5 +1,5 @@ --- -- name: "vmid {{ clone_result.vmid }}: set up static ip address" +- name: "vmid {{ clone_result.vmid }}: set up static ip address on windows" win_shell: | Start-Transcript -Path C:\set_domain_network_log.txt -Append Get-NetIpAddress -InterfaceAlias 'Ethernet' | Remove-NetIPAddress -Confirm:$false @@ -10,6 +10,56 @@ async: 15 poll: 0 delegate_to: "{{ vm_ip }}" + when: os_type == 'windows' + vars: + ansible_connection: "{{ win_connector }}" + ansible_user: "{{ default_win_username }}" + ansible_password: "{{ default_win_password }}" + ansible_port: "{{ win_port }}" + ansible_winrm_transport: basic + ansible_winrm_server_cert_validation: ignore + +- name: "vmid {{ vmid }}: configure network for linux" + block: + - name: "vmid {{ vmid }}: get default interface" + ansible.builtin.shell: ip route get 8.8.8.8 | sed -n 's/.*dev \([^\ ]*\).*/\1/p' + register: interface_result + + - name: "vmid {{ vmid }}: set default interface variable" + ansible.builtin.set_fact: + linux_interface_name: "{{ interface_result.stdout }}" + netplan_ip_address: "{{ ip }}" + + - name: "vmid {{ vmid }}: find netplan configs in /etc/netplan" + ansible.builtin.find: + paths: /etc/netplan + recurse: yes + register: yaml_configs + + - name: "vmid {{ vmid }}: remove all netplan configs in /etc/netplan" + ansible.builtin.file: + path: "{{ item.path }}" + state: absent + loop: "{{ yaml_configs.files }}" + + - name: "vmid {{ vmid }}: set netplan static ip address" + ansible.builtin.template: + src: static_ip_netplan.yaml.j2 + dest: /etc/netplan/01-netcfg.yaml + mode: '0644' + + - name: "vmid {{ vmid }}: apply netplan configuration" + ansible.builtin.command: netplan apply + async: 15 + poll: 0 + delegate_to: "{{ vm_ip }}" + when: os_type == 'linux' + vars: + ansible_connection: "{{ linux_connector }}" + ansible_user: "{{ default_linux_username }}" + ansible_password: "{{ default_linux_password }}" + ansible_port: "{{ linux_port }}" + ansible_host_key_checking: false - name: "vmid {{ clone_result.vmid }}: update ip to {{ ip }}" set_fact: diff --git a/ansible/roles/proxmox_vm/tasks/upload_files.yaml b/ansible/roles/proxmox_vm/tasks/upload_files.yaml index 651d203..f2b9df8 100644 --- a/ansible/roles/proxmox_vm/tasks/upload_files.yaml +++ b/ansible/roles/proxmox_vm/tasks/upload_files.yaml @@ -7,3 +7,4 @@ - { src: ../../../scripts/, dest: C:\scripts\ } - { src: ../../../files/software/, dest: C:\software\ } delegate_to: "{{ ansible_host }}" + when: os_type == 'windows' diff --git a/ansible/roles/srv01/tasks/join_domain.yaml b/ansible/roles/srv01/tasks/join_domain.yaml new file mode 100644 index 0000000..cd9b4f5 --- /dev/null +++ b/ansible/roles/srv01/tasks/join_domain.yaml @@ -0,0 +1,4 @@ +- name: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }}: execute join-domain.sh" + script: ../../../scripts/join-domain.sh -d "{{ main_domain_name }}" -n "{{ main_dc01_ip_address }}" -p "{{ default_win_password }}" + args: + executable: /bin/bash diff --git a/ansible/roles/srv01/tasks/main.yaml b/ansible/roles/srv01/tasks/main.yaml new file mode 100644 index 0000000..06f6974 --- /dev/null +++ b/ansible/roles/srv01/tasks/main.yaml @@ -0,0 +1,14 @@ +- name: wait for ssh to be available + ansible.builtin.wait_for: + host: "{{ ansible_host }}" + port: "{{ ansible_port }}" + timeout: 300 + delegate_to: localhost + vars: + ansible_connection: local + +- name: set hostname + import_tasks: set_hostname.yaml + +- name: execute join-domain.sh + import_tasks: join_domain.yaml diff --git a/ansible/roles/srv01/tasks/reboot.yaml b/ansible/roles/srv01/tasks/reboot.yaml new file mode 100644 index 0000000..29ea4b0 --- /dev/null +++ b/ansible/roles/srv01/tasks/reboot.yaml @@ -0,0 +1,5 @@ +- name: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }}: reboot" + command: "shutdown -r now &" + async: 1 + poll: 0 + ignore_errors: true diff --git a/ansible/roles/srv01/tasks/set_hostname.yaml b/ansible/roles/srv01/tasks/set_hostname.yaml new file mode 100644 index 0000000..40a9a8e --- /dev/null +++ b/ansible/roles/srv01/tasks/set_hostname.yaml @@ -0,0 +1,5 @@ +- name: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }}: update /etc/hosts" + ansible.builtin.replace: + path: /etc/hosts + regexp: "ubuntu-server2404" + replace: "{{ main_linux_srv01_hostname }}.{{ main_domain_name }} {{ main_linux_srv01_hostname }}" diff --git a/ansible/roles/srv01/templates/static_ip_netplan.yaml.j2 b/ansible/roles/srv01/templates/static_ip_netplan.yaml.j2 new file mode 100644 index 0000000..03463b1 --- /dev/null +++ b/ansible/roles/srv01/templates/static_ip_netplan.yaml.j2 @@ -0,0 +1,10 @@ +network: + version: 2 + ethernets: + {{ linux_interface_name }}: + addresses: + - {{ netplan_ip_address }}/24 + gateway4: {{ network_gateway }} + nameservers: + addresses: + - {{ main_dc01_ip_address }} -- cgit v1.2.3