Excecute command in vSphere Guest machine with Ansible

2.9k views Asked by At

I am trying to change the IP of a Windows Server 2012R2 vmware guest machine with the vmware_vm_shell module. So far I have been able to clone the VM from a template with the vsphere_guest module without problems, but when I try the following task to change the IP:

- name: Configure IP address
  local_action:
    module: vmware_vm_shell
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_password }}"
    cluster: "{{ Cluster }}"
    vm_id: "{{ machine }}"
    vm_username: "{{ machineUser }}"
    vm_password: "{{ ansible_windows_password  }}"
    vm_shell: "C:\\Windows\\System32\\WindowsPowershell\\1.0\\powershell.exe"
    vm_shell_args: " -command (Get-NetAdapter -Name Ethernet |New-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 -IPAddress {{ new_machine_ip }} -PrefixLength {{ new_machine_mask_bits }} -DefaultGateway {{ new_machine_gateway }} )-and (Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses {{ new_machine_dns }})"
    vm_shell_cwd: "C:\\Windows\\Temp"
    validate_certs: False

The response from Ansible is "msg": "File <unspecified filename> was not found".

I get the same error when I try to run the execute_program_in_vm.py script from pyvmomi-community-samples: Caught vmodl fault : File <unspecified filename> was not found

The vmware user I have has all the Guest Operations permissions set. What am I missing here?

PS: I followed this guide.

1

There are 1 answers

5
Tem On

Try it again with this task I made. This module has sometimes problems with parsing powershell commands. At least that's the experience I had. I don't know exactly why my command is working and yours is not. I replaced the command in "vm_shell_args".

You just have to replace the values in the square brackets.

Edit: Also try the playbook again but with the variables written without the spaces (e.g. "{{vcenter_hostname}}" instead of "{{ vcenter_hostname }}"

  - name: change IP
    local_action:
      module: vmware_vm_shell
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_user }}"
      password: "{{ vcenter_password }}"
      cluster: "{{ Cluster }}"
      vm_id: "{{ machine }}"
      vm_username: "{{ machineUser }}"
      vm_password: "{{ ansible_windows_password  }}"
      vm_shell: 'C:\Windows\System32\WindowsPowershell\1.0\powershell.exe'
      vm_shell_args: 'netsh interface ip set address [Interface name] static [New IP] [Subnetmask] [Gateway]'