Score:2

How to set VMWare VM screen resolution on Windows using Ansible

be flag

I'm trying to deploy a vSphere Windows VM via Ansible and need to set a specific screen resolution (1024x768). Running VMWareResolutionSet.exe works locally in PowerShell with the following command (the , needs to be escaped with a ` in Powershell to avoid making the arguments a list, and the & is needed to run commands with spaces in their paths):

& "C:\Program Files\VMWare\VMware Tools\VMwareResolutionSet.exe" 0 1 `, 0 0 1024 768

However, running this command remotely with Ansible's win_command only yields a return code of 1 with no further error message. The same behavior occurs when running the command directly with pywinrm or when invoking PowerShell as a subshell. As far as I can tell, the problem lies with this not being an interactive PowerShell instance. However, setting become: true and become_method: runas did not work.

How can I set the VM screen resolution via Ansible?

Score:1
be flag

Ultimately I was unable to get this to run remotely via PowerShell, but I was able to run this as a scheduled task which runs immediately upon being registered:

- name: Ensure that the user can run scheduled tasks
  win_user_right:
    name: "SeBatchLogonRight"
    action: add
    users:
      - "{{ ansible_user }}"

# Deleting and reregistering will force the task to run every time
- name: Remove screen resolution task if present
  win_scheduled_task:
    name: SetScreenResolution
    state: absent

- name: Create a task to set screen resolution
  win_scheduled_task:
    name: SetScreenResolution
    description: Set the screen resolution
    actions:
      - path: C:\Program Files\VMWare\VMware Tools\VMwareResolutionSet.exe
        arguments: "0 1 , 0 0 1024 768"
    triggers:
      - type: registration
    state: present
    enabled: true

- name: Wait for the scheduled task to complete
  win_scheduled_task_stat:
    name: SetScreenResolution
  register: task_stat
  until: (task_stat.state is defined and task_stat.state.status != "TASK_STATE_RUNNING") or (task_stat.task_exists == False)
  retries: 5

you can configure this task to delete itself using the instructions found in https://stackoverflow.com/questions/64006824/ansible-win-scheduled-task-how-to-start-a-task-immediately. I still suspect there's a way to do this with win_command with the right setting, but I wasn't able to figure it out.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.