我想在自定义过程中在虚拟机(Windows 和 Linux)上设置密码,因此使用 vmware_guest 模块中的“自定义”选项来实现此目的。它非常适合从 Windows 模板部署的 VM,但不适用于 Linux VM。任何帮助将不胜感激。
代码:
customization:
hostname: "{{ vm_name }}"
password: "{{ my_pass }}"
wait_for_customization: yes
wait_for_ip_address: True
state: poweredon
在互联网上搜索时发现 VMware 社区线程说无法自定义 linux root 密码,但还有其他博客迭代了相同的内容。我可能不得不使用 ansible 中的用户模块作为解决方法。 VMware 社区主题:https://communities.vmware.com/thread/252997
不,你可以用这个做到这一点
首先通过ansible任务获取你创建的虚拟机的ID
- name: Get VM MoID
community.vmware.vmware_vm_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
vm_name: "{{ vm_name }}"
register: vm_info_result
delegate_to: localhost
- name: Get VM MoID
community.vmware.vmware_vm_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
vm_name: "{{ vm_name }}"
register: vm_info_result
delegate_to: localhost
将虚拟机的 id 作为事实
- name: Set VM MoID fact
set_fact:
vm_moid: "{{ vm_info_result.virtual_machines[0].moid }}"
delegate_to: localhost
现在创建根任务,使用虚拟机 ID 来了解哪个虚拟机需要通过 vmware 工具发送 shell,您在创建模板时需要初始密码,以将其替换为您想要的新密码
- name: Change root password
community.vmware.vmware_vm_shell:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
vm_id: "{{ vm_name }}"
vm_username: 'root'
vm_password: "{{ initial_root_password }}"
vm_shell: /bin/bash
vm_shell_args: "-c 'echo root:{{ new_root_password }} | chpasswd'"
delegate_to: localhost
when: vm_creation_result is changed