Deploy Windows VM through Ansible | Nutanix Community
Skip to main content

Trying to deploy a VM through ansible playbook.  Deployment work fine. But I would like to configure IP address, netmask and gateway. Also I would like to add code for joining the vm to AD domain. Looked into guest_customization. But I am not finding any documentation on how to use guest_customization. There is some, but it only for Linux systems. Any tips/references will be appreciated.

 

Thanks

 

Yezdi

Hi Yezdi,

From the ansible documentation by running ansible-doc nutanix.ncp.ntnx_vms

- guest_customization
cloud_init or sysprep guest customization
"default": !!null "null"
"type": "dict"

SUBOPTIONS:

- is_overridable
Flag to allow override of customization during deployment.
"default": !!bool "false"
"type": "bool"

= script_path
Absolute file path to the script.
"type": "path"

= type
cloud_init or sysprep type
choices: "cloud_init", "sysprep"]
"type": "str"

Make sure to have your sysprep file prepared to provide it in the script_path. Any customization you want to make to the sysprep file will have to go through a template with jinja2


Thanks for taking time and responding to my question. I built an sysprepped image in Nutanix. Then also built an unattended.xml file with Windows SIM and ADK. I was able to deploy a VM with IP address successfully. Initially I was looking for some inline code in the playbook itself. But it seems it’s not possible.

 

For joining the node to AD domain, I used WMIC through FirstLogonCommands, but it attaches the VM to a workgroup, not domain. Also I need to specify username and passwd in the XML file which is not ideal. I was wondering what the best way to do this ?

 ---
- name: Windows 2022 VM playbook
hosts: localhost
gather_facts: false
vars:
vm_name: "MyWin2022Server"
collections:
- nutanix.ncp
module_defaults:
group/nutanix.ncp.ntnx:
nutanix_host: "{{ pc_ip }}"
nutanix_username: "{{ vault_pc_username }}"
nutanix_password: "{{ vault_pc_password }}"
validate_certs: false
tasks:
- name: List vms using name filter criteria
ntnx_vms_info:
filter:
vm_name: "MyWin2022Server"
kind: vm
register: existing_vms
ignore_errors: True
- debug:
msg: "{{ existing_vms | json_query('response.entities
  • .spec.name') }}"

    - name: Check if VM already exists
    set_fact:
    vm_already_exists: "{{ existing_vms | json_query('response.entities
  • .spec.name') }}"

    - debug:
    msg: "{{ vm_already_exists }}"

    - name: Setting Variables
    set_fact:
    cluster_name: "MyNutanixCluster"
    subnet_name: "MyVLAN-100"
    image_name: "WindowsServerImage2022"
    script_path: "/root/win2022/new.xml"
    - name: create Vm
    ntnx_vms:
    state: present
    name: "MyWin2022Server"
    desc: "Windows Server 2022 Standard"
    cluster:
    name: "{{cluster_name}}"
    networks:
    - is_connected: True
    subnet:
    name: "{{ subnet_name }}"
    disks:
    - type: "DISK"
    size_gb: 100
    bus: "SATA"
    clone_image:
    name: "{{ image_name }}"
    vcpus: 1
    cores_per_vcpu: 1
    memory_gb: 1
    guest_customization:
    type: "sysprep"
    script_path: "{{ script_path }}"
    is_overridable: True
    register: output
    when: not vm_already_exists

    - name: output of vm created
    debug:
    msg: '{{ output }}'
  •  


    That’s how Windows customization works, it’s not specific to Nutanix Ansible collection. Have a look to this sysprep file, in this case the variable parameters are for Terraform, but you can get an idea. Sysprep has builting AD join capability, you don’t have to do it at the FirstLogonCommand section.

    Another example https://portal.nutanix.com/page/documents/details?targetId=Self-Service-Admin-Operations-Guide-v3_7_2_1:nuc-scripts-configure-non-managed-ahv-network-c.html.


    Thanks again Jose. I tried to build a new unattended.xml file with “Microsoft-Windows-UnattendedJoin”. But it doesn’t seem to encrypt the passwd. It leaves it in clear text.


    That’s expected, please read more about how it works in the Microsoft documentation.


    Awesome, I will read through the documentation.