我正在尝试通过 Ansible 部署 vSphere Windows VM,并且需要设置特定的屏幕分辨率(1024x768)。正在运行VMWareResolutionSet.exe
使用以下命令在 PowerShell 中本地工作(,
在 Powershell 中需要用 ` 进行转义以避免使参数成为列表,并且需要&
运行路径中带有空格的命令):
& "C:\Program Files\VMWare\VMware Tools\VMwareResolutionSet.exe" 0 1 `, 0 0 1024 768
但是,使用 Ansible 的win_command
只会产生返回代码,1
没有进一步的错误消息。直接使用以下命令运行命令时也会出现相同的行为pywinrm
或者在将 PowerShell 作为子 shell 调用时。据我所知,问题在于这不是一个交互式 PowerShell 实例。但是,设置become: true
和become_method: runas
不起作用。
如何通过 Ansible 设置 VM 屏幕分辨率?
答案1
最终,我无法通过 PowerShell 远程运行它,但我曾是可以将其作为计划任务运行,注册后立即运行:
- 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
您可以按照以下说明配置此任务以删除自身https://stackoverflow.com/questions/64006824/ansible-win-scheduled-task-how-to-start-a-task-immediately。我仍然怀疑有一种方法可以通过win_command
正确的设置来做到这一点,但我无法弄清楚。