community.vmware.vmware_vm_shell 模块 – 在 VMware 客户操作系统中运行命令

注意

此模块是 community.vmware 集合(版本 5.2.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.vmware

要在 playbook 中使用它,请指定:community.vmware.vmware_vm_shell

概要

  • 该模块允许用户在客户操作系统中运行常见的系统管理命令。

参数

参数

注释

集群

字符串

托管虚拟机的集群。

如果设置,它将有助于加快虚拟机搜索速度。

数据中心

字符串

托管虚拟机的数据中心。

如果设置,它将有助于加快虚拟机搜索速度。

文件夹

字符串

目标文件夹,用于查找现有客户机或创建新客户机的绝对或相对路径。

该文件夹应包括数据中心。ESX 的数据中心是 ha-datacenter。

示例

folder: /ha-datacenter/vm

folder: ha-datacenter/vm

folder: /datacenter1/vm

folder: datacenter1/vm

folder: /datacenter1/vm/folder1

folder: datacenter1/vm/folder1

folder: /folder1/datacenter1/vm

folder: folder1/datacenter1/vm

folder: /folder1/datacenter1/vm/folder2

如果 vm_id_type=inventory_path,则为必需项

主机名

字符串

vSphere vCenter 或 ESXi 服务器的主机名或 IP 地址。

如果未在任务中指定该值,则将改用环境变量 VMWARE_HOST 的值。

密码

别名:pass, pwd

字符串

vSphere vCenter 或 ESXi 服务器的密码。

如果未在任务中指定该值,则将改用环境变量 VMWARE_PASSWORD 的值。

端口

整数

vSphere vCenter 或 ESXi 服务器的端口号。

如果未在任务中指定该值,则将改用环境变量 VMWARE_PORT 的值。

默认值: 443

代理主机

字符串

将接收所有 HTTPS 请求并转发它们的代理的地址。

格式为主机名或 IP。

如果未在任务中指定该值,则将改用环境变量 VMWARE_PROXY_HOST 的值。

代理端口

整数

将接收所有 HTTPS 请求并转发它们的 HTTP 代理的端口。

如果未在任务中指定该值,则将改用环境变量 VMWARE_PROXY_PORT 的值。

超时

整数

以秒为单位的超时时间。

如果设置为正整数,则 wait_for_process=true 将遵守此参数,并在此超时后退出。

默认值: 3600

用户名

别名:admin, user

字符串

vSphere vCenter 或 ESXi 服务器的用户名。

如果未在任务中指定该值,则将改用环境变量 VMWARE_USER 的值。

验证证书

布尔值

允许在 SSL 证书无效时进行连接。当证书不受信任时,设置为 false

如果未在任务中指定该值,则将改用环境变量 VMWARE_VALIDATE_CERTS 的值。

选择

  • false

  • true ← (默认)

vm_id

string / 必需

要操作的虚拟机的名称。

vm_id_type

字符串

用于识别虚拟机的 VMware 识别方法。

选择

  • "uuid"

  • "instance_uuid"

  • "dns_name"

  • "inventory_path"

  • "vm_name" ← (默认)

vm_password

string / 必需

用于登录虚拟机的密码。

vm_shell

string / 必需

要启动的程序的绝对路径。

在 Linux 上,shell 通过 bash 执行。

vm_shell_args

字符串

程序的参数。

必须转义到 shell 的字符也必须在提供的命令行中进行转义。

默认值: " "

vm_shell_cwd

字符串

应用程序将要运行的当前工作目录。

vm_shell_env

list / 元素=string

以客户操作系统表示法指定的,以逗号分隔的环境变量列表。

vm_username

string / 必需

用于登录虚拟机的用户。

wait_for_process

布尔值

如果设置为 true,模块将等待给定虚拟机中的进程完成。

选择

  • false ← (默认)

  • true

说明

注意

  • 即使有多个匹配项,也仅使用与 vm_id 的第一个匹配项。

  • 所有模块都需要 API 写入访问权限,因此不支持免费的 ESXi 许可证。

  • 所有变量和 VMware 对象名称都区分大小写。

示例

- name: Run command inside a virtual machine
  community.vmware.vmware_vm_shell:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter }}"
    folder: "/{{datacenter}}/vm"
    vm_id: "{{ vm_name }}"
    vm_username: root
    vm_password: superSecret
    vm_shell: /bin/echo
    vm_shell_args: " $var >> myFile "
    vm_shell_env:
      - "PATH=/bin"
      - "VAR=test"
    vm_shell_cwd: "/tmp"
  delegate_to: localhost
  register: shell_command_output

- name: Run command inside a virtual machine with wait and timeout
  community.vmware.vmware_vm_shell:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter }}"
    folder: "/{{datacenter}}/vm"
    vm_id: NameOfVM
    vm_username: root
    vm_password: superSecret
    vm_shell: /bin/sleep
    vm_shell_args: 100
    wait_for_process: true
    timeout: 2000
  delegate_to: localhost
  register: shell_command_with_wait_timeout

- name: Change user password in the guest machine
  community.vmware.vmware_vm_shell:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter }}"
    folder: "/{{datacenter}}/vm"
    vm_id: "{{ vm_name }}"
    vm_username: sample
    vm_password: old_password
    vm_shell: "/bin/echo"
    vm_shell_args: "-e 'old_password\nnew_password\nnew_password' | passwd sample > /tmp/$$.txt 2>&1"
  delegate_to: localhost

- name: Change hostname of guest machine
  community.vmware.vmware_vm_shell:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter: "{{ datacenter }}"
    folder: "/{{datacenter}}/vm"
    vm_id: "{{ vm_name }}"
    vm_username: testUser
    vm_password: SuperSecretPassword
    vm_shell: "/usr/bin/hostnamectl"
    vm_shell_args: "set-hostname new_hostname > /tmp/$$.txt 2>&1"
  delegate_to: localhost

返回值

通用返回值记录在 这里,以下是此模块特有的字段

描述

results

字典

在使用 wait_for_process 完成后,有关新进程的元数据

返回: 成功时

示例: {"cmd_line": "\"/bin/sleep\" 1", "end_time": "2018-04-26T05:03:21+00:00", "exit_code": 0, "name": "sleep", "owner": "dev1", "start_time": "2018-04-26T05:03:19+00:00", "uuid": "564db1e2-a3ff-3b0e-8b77-49c25570bb66"}

作者

  • Ritesh Khadgaray (@ritzk)

  • Abhijeet Kasurde (@Akasurde)