使用 Packer 自动创建模板并在 VMware vSphere 环境中使用 Ansible 部署¶
知识:
复杂度:
阅读时间: 30 分钟
先决条件、假设和一般说明¶
- vSphere 环境可用,并且有一个具有授权访问权限的用户
- 一个用于存储文件的内部 Web 服务器
- 对 Rocky Linux 存储库的 Web 访问
- Rocky Linux 的 ISO 映像
- Ansible 环境可用
- 假设您对提到的每个产品都有一定的了解。如果没有,请在开始之前深入了解这些文档。
- 此处不使用 Vagrant。有人指出,使用 Vagrant 时,将提供一个非自签名的 SSH 密钥。如果您想深入了解,可以这样做,但这不在本文档的涵盖范围之内。
简介¶
本文档介绍了使用 Packer 创建 vSphere 虚拟机模板,以及如何使用 Ansible 部署工件作为新的虚拟机。
可能的调整¶
当然,您可以将此操作方法调整为其他虚拟化程序。
虽然我们在此处使用最小 ISO 映像,但您可以选择使用 DVD 映像(更大,可能太大)或启动映像(更小,可能太小)。这个选择由您决定。它特别影响您安装所需的带宽,因此也影响预配时间。接下来我们将讨论默认选择的影響,以及如何解决它。
您也可以选择不将虚拟机转换为模板。在这种情况下,您将使用 Packer 部署每个新 VM,这仍然是可行的。从 0 开始的安装在没有人工干预的情况下不到 10 分钟即可完成。
Packer¶
Packer 简介¶
Packer 是一个开源虚拟机镜像工具,在 MPL 2.0 许可证下发布,由 HashiCorp 创建。它将帮助您在云和本地虚拟化环境中,从单个源配置自动执行创建预配置操作系统和已安装软件的虚拟机镜像的过程。
使用 Packer,您可以创建要在以下平台上使用的镜像
您可以查看以下资源以获取更多信息
安装 Packer¶
在 Rocky Linux 系统上安装 Packer 有两种方法。
从 HashiCorp 存储库安装 Packer¶
HashiCorp 为不同的 Linux 发行版维护并签署软件包。要在我们的 Rocky Linux 系统中安装 packer,请按照以下步骤操作
安装 dnf-config-manager
sudo dnf install -y dnf-plugins-core
将 HashiCorp 存储库添加到我们 Rocky Linux 系统中可用的存储库
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
安装 Packer
sudo dnf -y install packer
从 Packer 网站下载并安装¶
您可以从Packer 下载开始下载您自己平台的二进制文件。
在下载页面上,复制 Linux Binary Download 部分中与您的系统架构相对应的下载链接。
从 shell 或终端使用
wget
工具下载它wget https://releases.hashicorp.com/packer/1.8.3/packer_1.8.3_linux_amd64.zip
这将下载一个 .zip 文件。
要解压缩下载的归档文件,请在 shell 中运行以下命令
unzip packer_1.8.3_linux_amd64.zip
提示
如果您收到错误并且您的系统上没有安装 unzip 应用程序,您可以通过执行此命令
sudo dnf install unzip
来安装它。将 Packer 应用程序移动到 bin 文件夹
sudo mv packer /usr/local/bin/
验证 Packer 安装是否正确¶
如果前面的所有步骤都已正确完成,我们就可以验证 Packer 在系统上的安装情况。
要验证 Packer 是否已正确安装,请运行packer
命令,您将得到如下所示的结果
$ packer
Usage: packer [--version] [--help] <command> [<args>]
Available commands are:
build build image(s) from template
console creates a console for testing variable interpolation
fix fixes templates from old versions of packer
fmt rewrites HCL2 config files to canonical format
hcl2_upgrade transform a JSON template into an HCL2 configuration
init install missing plugins or upgrade plugins
inspect see components of a template
plugins interact with Packer plugins and catalog
validate check that a template is valid
version prints the Packer version
使用 Packer 创建模板¶
注意
以下示例假设您使用的是 Linux 系统。
由于我们将连接到 VMware vCenter Server 通过 Packer 发送命令,因此我们需要将我们的凭据存储在稍后将创建的配置文件之外。
让我们在主目录中创建一个包含我们凭据的隐藏文件。这是一个 json 文件
$ vim .vsphere-secrets.json {
"vcenter_username": "rockstar",
"vcenter_password": "mysecurepassword"
}
这些凭据需要对您的 vSphere 环境进行一些授权访问。
让我们创建一个 json 文件(将来,此文件的格式将更改为 HCL)
{
"variables": {
"version": "0.0.X",
"HTTP_IP": "fileserver.rockylinux.lan",
"HTTP_PATH": "/packer/rockylinux/8/ks.cfg"
},
"sensitive-variables": ["vcenter_password"],
"provisioners": [
{
"type": "shell",
"expect_disconnect": true,
"execute_command": "bash '{{.Path}}'",
"script": "{{template_dir}}/scripts/requirements.sh"
}
],
"builders": [
{
"type": "vsphere-iso",
"CPUs": 2,
"CPU_hot_plug": true,
"RAM": 2048,
"RAM_hot_plug": true,
"disk_controller_type": "pvscsi",
"guest_os_type": "centos8_64Guest",
"iso_paths": [
"[datasyno-contentlibrary-mylib] contentlib-a86ad29a-a43b-4717-97e6-593b8358801b/3a381c78-b9df-45a6-82e1-3c07c8187dbe/Rocky-8.4-x86_64-minimal_72cc0cc6-9d0f-4c68-9bcd-06385a506a5d.iso"
],
"network_adapters": [
{
"network_card": "vmxnet3",
"network": "net_infra"
}
],
"storage": [
{
"disk_size": 40000,
"disk_thin_provisioned": true
}
],
"boot_command": [
"<up><tab> text ip=192.168.1.11::192.168.1.254:255.255.255.0:template:ens192:none nameserver=192.168.1.254 inst.ks=http://{{ user `HTTP_IP` }}/{{ user `HTTP_PATH` }}<enter><wait><enter>"
],
"ssh_password": "mysecurepassword",
"ssh_username": "root",
"shutdown_command": "/sbin/halt -h -p",
"insecure_connection": "true",
"username": "{{ user `vcenter_username` }}",
"password": "{{ user `vcenter_password` }}",
"vcenter_server": "vsphere.rockylinux.lan",
"datacenter": "DC_NAME",
"datastore": "DS_NAME",
"vm_name": "template-rockylinux8-{{ user `version` }}",
"folder": "Templates/RockyLinux",
"cluster": "CLUSTER_NAME",
"host": "esx1.rockylinux.lan",
"notes": "Template RockyLinux version {{ user `version` }}",
"convert_to_template": true,
"create_snapshot": false
}
]
}
接下来,我们将描述此文件的每个部分。
变量部分¶
在第一步中,我们声明变量,主要为了提高可读性
"variables": {
"version": "0.0.X",
"HTTP_IP": "fileserver.rockylinux.lan",
"HTTP_PATH": "/packer/rockylinux/8/ks.cfg"
},
我们将在稍后创建的模板名称中使用变量 version
。您可以轻松地递增此值以满足您的需求。
我们还需要引导虚拟机来访问 ks.cfg
(Kickstart)文件。
Kickstart 文件包含安装过程中提出的问题的答案。此文件将其所有内容传递给 Anaconda(安装过程),这使您可以完全自动化模板的创建。
作者喜欢将他的 ks.cfg
文件存储在模板可访问的内部 Web 服务器中,但您也可以选择使用其他可能性。
例如,ks.cfg
文件从以下 URL 在我们的实验室的 VM 中可访问:http://fileserver.rockylinux.lan/packer/rockylinux/8/ks.cfg。您需要设置类似的东西才能使用此方法。
由于我们希望将密码保密,因此将其声明为敏感变量。示例
"sensitive-variables": ["vcenter_password"],
Provisioners 部分¶
下一部分很有趣,稍后将通过提供 requirements.sh
的脚本进行介绍
"provisioners": [
{
"type": "shell",
"expect_disconnect": true,
"execute_command": "bash '{{.Path}}'",
"script": "{{template_dir}}/scripts/requirements.sh"
}
],
安装完成后,VM 将重新启动。一旦 Packer 检测到 IP 地址(感谢 VMware Tools),它将复制 requirements.sh
并执行它。这是一个不错的功能,可以在安装过程后清理 VM(删除 SSH 密钥,清理历史记录等)并安装一些额外的软件包。
构建器部分¶
您可以声明一个或多个构建器来定位除 vSphere 环境之外的其他目标(可能是 Vagrant 模板)。
但这里我们使用 vsphere-iso
构建器
"builders": [
{
"type": "vsphere-iso",
此构建器使我们能够配置所需的硬件
"CPUs": 2,
"CPU_hot_plug": true,
"RAM": 2048,
"RAM_hot_plug": true,
"disk_controller_type": "pvscsi",
"guest_os_type": "centos8_64Guest",
"network_adapters": [
{
"network_card": "vmxnet3",
"network": "net_infra"
}
],
"storage": [
{
"disk_size": 40000,
"disk_thin_provisioned": true
}
],
!!! "注意"
You will never forget again to include CPU_hot_plug as it is automatic now!
您还可以使用磁盘、cpu 等做更多有趣的事情。如果您有兴趣进行其他调整,请参阅文档。
要启动安装,您需要 Rocky Linux 的 ISO 映像。以下是如何使用位于 vSphere 内容库中的映像的示例。您当然可以将 ISO 存储在其他位置。对于 vSphere 内容库,您必须获取托管内容库的服务器上 ISO 文件的完整路径。在本例中,它是 Synology,因此直接在 DSM 资源管理器中。
"iso_paths": [
"[datasyno-contentlibrary-mylib] contentlib-a86ad29a-a43b-4717-97e6-593b8358801b/3a381c78-b9df-45a6-82e1-3c07c8187dbe/Rocky-8.4-x86_64-minimal_72cc0cc6-9d0f-4c68-9bcd-06385a506a5d.iso"
],
然后,您必须提供在安装过程中输入的完整命令:IP 的配置和到 Kickstart 响应文件的路径的传输。
注意
此示例采用最复杂的情况:使用静态 IP。如果您有 DHCP 服务器可用,该过程将变得容易得多。
这是过程中最有趣的部分:我相信您会在生成过程中去欣赏 VMware 控制台,只是为了看到在引导期间自动输入命令。
"boot_command": [
"<up><tab> text ip=192.168.1.11::192.168.1.254:255.255.255.0:template:ens192:none nameserver=192.168.1.254 inst.ks=http://{{ user `HTTP_IP` }}/{{ user `HTTP_PATH` }}<enter><wait><enter>"
],
第一次重启后,Packer 将通过 SSH 连接到您的服务器。您可以使用 root 用户或其他具有 sudo 权限的用户,但无论哪种情况,此用户必须与您的 ks.cfg 文件中定义的用户相对应。
"ssh_password": "mysecurepassword",
"ssh_username": "root",
在流程结束时,VM 必须停止。使用非 root 用户会稍微复杂一些,但这已在文档中进行了详细说明
"shutdown_command": "/sbin/halt -h -p",
接下来,我们处理 vSphere 配置。这里唯一值得注意的是使用我们主目录中文档开头定义的变量,以及 insecure_connection
选项,因为我们的 vSphere 使用自签名证书(请参阅本文档顶部的假设中的说明)
"insecure_connection": "true",
"username": "{{ user `vcenter_username` }}",
"password": "{{ user `vcenter_password` }}",
"vcenter_server": "vsphere.rockylinux.lan",
"datacenter": "DC_NAME",
"datastore": "DS_NAME",
"vm_name": "template-rockylinux8-{{ user `version` }}",
"folder": "Templates/RockyLinux",
"cluster": "CLUSTER_NAME",
"host": "esx1.rockylinux.lan",
"notes": "Template RockyLinux version {{ user `version` }}"
最后,我们将要求 vSphere 将我们停止的 VM 转换为模板。
在此阶段,您也可以选择直接使用 VM(不将其转换为模板)。在这种情况下,您可以决定取而代之的是拍摄快照
"convert_to_template": true,
"create_snapshot": false,
ks.cfg 文件¶
如上所述,我们需要提供一个 Kickstart 响应文件,该文件将由 Anaconda 使用。
以下是一个示例文件
# Use CD-ROM installation media
repo --name="AppStream" --baseurl="http://download.rockylinux.org/pub/rocky/8.4/AppStream/x86_64/os/"
cdrom
# Use text install
text
# Don't run the Setup Agent on first boot
firstboot --disabled
eula --agreed
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
# Network information
network --bootproto=static --device=ens192 --gateway=192.168.1.254 --ip=192.168.1.11 --nameserver=192.168.1.254,4.4.4.4 --netmask=255.255.255.0 --onboot=on --ipv6=auto --activate
# Root password
rootpw mysecurepassword
# System services
selinux --permissive
firewall --enabled
services --enabled="NetworkManager,sshd,chronyd"
# System timezone
timezone Europe/Paris --isUtc
# System booloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Disk partitionning information
part /boot --fstype="xfs" --ondisk=sda --size=512
part pv.01 --fstype="lvmpv" --ondisk=sda --grow
volgroup vg_root --pesize=4096 pv.01
logvol /home --fstype="xfs" --size=5120 --name=lv_home --vgname=vg_root
logvol /var --fstype="xfs" --size=10240 --name=lv_var --vgname=vg_root
logvol / --fstype="xfs" --size=10240 --name=lv_root --vgname=vg_root
logvol swap --fstype="swap" --size=4092 --name=lv_swap --vgname=vg_root
skipx
reboot
%packages --ignoremissing --excludedocs
openssh-clients
curl
dnf-utils
drpm
net-tools
open-vm-tools
perl
perl-File-Temp
sudo
vim
wget
python3
# unnecessary firmware
-aic94xx-firmware
-atmel-firmware
-b43-openfwwf
-bfa-firmware
-ipw2100-firmware
-ipw2200-firmware
-ivtv-firmware
-iwl*-firmware
-libertas-usb8388-firmware
-ql*-firmware
-rt61pci-firmware
-rt73usb-firmware
-xorg-x11-drv-ati-firmware
-zd1211-firmware
-cockpit
-quota
-alsa-*
-fprintd-pam
-intltool
-microcode_ctl
%end
%addon com_redhat_kdump --disable
%end
%post
# Manage Ansible access
groupadd -g 1001 ansible
useradd -m -g 1001 -u 1001 ansible
mkdir /home/ansible/.ssh
echo -e "<---- PAST YOUR PUBKEY HERE ---->" > /home/ansible/.ssh/authorized_keys
chown -R ansible:ansible /home/ansible/.ssh
chmod 700 /home/ansible/.ssh
chmod 600 /home/ansible/.ssh/authorized_keys
echo "ansible ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers.d/ansible
chmod 440 /etc/sudoers.d/ansible
systemctl enable vmtoolsd
systemctl start vmtoolsd
%end
由于我们选择使用最小 ISO,而不是 Boot 或 DVD,因此并非所有必需的安装软件包都可用。
由于 Packer 依赖于 VMware Tools 来检测安装结束,而 open-vm-tools
软件包仅在 AppStream 存储库中可用,因此我们必须在安装过程中指定我们要将 CD-ROM 和此远程存储库用作源
!!! "注意"
If you do not have access to the external repos, you can use either a mirror of the repo, a squid proxy, or the DVD.
# Use CD-ROM installation media
repo --name="AppStream" --baseurl="http://download.rockylinux.org/pub/rocky/8.4/AppStream/x86_64/os/"
cdrom
让我们跳到网络配置,因为同样,在这个例子中我们没有使用 DHCP 服务器
# Network information
network --bootproto=static --device=ens192 --gateway=192.168.1.254 --ip=192.168.1.11 --nameserver=192.168.1.254,4.4.4.4 --netmask=255.255.255.0 --onboot=on --ipv6=auto --activate
请记住,我们在安装结束时指定了通过 SSH 连接到 Packer 的用户。此用户和密码必须匹配
# Root password
rootpw mysecurepassword
警告
您可以在此处使用不安全的密码,只要您确保此密码在您的 VM 部署后立即更改即可,例如使用 Ansible。
以下是选择的分区方案。可以做更多复杂的事情。您可以定义适合您需求的分区方案,将其调整为 Packer 中定义的磁盘空间,并符合为您的环境定义的安全规则(/tmp
的专用分区等)
# System booloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel --drives=sda
# Disk partitionning information
part /boot --fstype="xfs" --ondisk=sda --size=512
part pv.01 --fstype="lvmpv" --ondisk=sda --grow
volgroup vg_root --pesize=4096 pv.01
logvol /home --fstype="xfs" --size=5120 --name=lv_home --vgname=vg_root
logvol /var --fstype="xfs" --size=10240 --name=lv_var --vgname=vg_root
logvol / --fstype="xfs" --size=10240 --name=lv_root --vgname=vg_root
logvol swap --fstype="swap" --size=4092 --name=lv_swap --vgname=vg_root
下一部分涉及将要安装的软件包。一个“最佳实践”是将安装的软件包数量限制为仅您需要的软件包,这限制了攻击面,尤其是在服务器环境中。
注意
作者喜欢将要在安装过程中执行的操作限制在最小范围内,并将安装所需内容延迟到 Packer 的安装后脚本中。因此,在本例中,我们仅安装了所需的最小软件包。
openssh-clients
软件包似乎是 Packer 将其脚本复制到 VM 中所必需的。
open-vm-tools
也需要 Packer 检测安装结束,这解释了添加 AppStream 存储库的原因。perl
和 perl-File-Temp
软件包在部署过程中也将需要 VMware Tools。这很糟糕,因为它需要许多其他依赖软件包。python3
(3.6) 在将来也将需要 Ansible 工作(如果您不使用 Ansible 或 python,请将其删除!)。
%packages --ignoremissing --excludedocs
openssh-clients
open-vm-tools
python3
perl
perl-File-Temp
curl
dnf-utils
drpm
net-tools
sudo
vim
wget
您不仅可以添加软件包,还可以删除软件包。由于我们控制了硬件将要工作的环境,因此我们可以删除对我们来说无用的任何固件
# unnecessary firmware
-aic94xx-firmware
-atmel-firmware
...
下一部分添加了一些用户。在本例中,创建一个没有密码但具有公钥的 ansible
用户很有趣。这允许我们所有新的 VM 从 Ansible 服务器访问以运行安装后操作
# Manage Ansible access
groupadd -g 1001 ansible
useradd -m -g 1001 -u 1001 ansible
mkdir /home/ansible/.ssh
echo -e "<---- PAST YOUR PUBKEY HERE ---->" > /home/ansible/.ssh/authorized_keys
chown -R ansible:ansible /home/ansible/.ssh
chmod 700 /home/ansible/.ssh
chmod 600 /home/ansible/.ssh/authorized_keys
echo "ansible ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers.d/ansible
chmod 440 /etc/sudoers.d/ansible
现在我们需要启用并启动 vmtoolsd
(管理 open-vm-tools 的进程)。VM 重启后,vSphere 将检测到 IP 地址。
systemctl enable vmtoolsd
systemctl start vmtoolsd
安装过程已完成,VM 将重新启动。
Provisioners¶
请记住,我们在 Packer 中声明了一个 provisioner,在本例中对应于 .sh
脚本,要存储在 json 文件旁边的子目录中。
有不同类型的 provisioner,我们也可以使用 Ansible。您可以自由探索这些可能性。
此文件可以完全更改,但这提供了使用脚本可以执行的操作示例,在本例中为 requirements.sh
#!/bin/sh -eux
echo "Updating the system..."
dnf -y update
echo "Installing cloud-init..."
dnf -y install cloud-init
# see https://bugs.launchpad.net/cloud-init/+bug/1712680
# and https://kb.vmware.com/s/article/71264
# Virtual Machine customized with cloud-init is set to DHCP after reboot
echo "manual_cache_clean: True " > /etc/cloud/cloud.cfg.d/99-manual.cfg
echo "Disable NetworkManager-wait-online.service"
systemctl disable NetworkManager-wait-online.service
# cleanup current SSH keys so templated VMs get fresh key
rm -f /etc/ssh/ssh_host_*
# Avoid ~200 meg firmware package we don't need
# this cannot be done in the KS file so we do it here
echo "Removing extra firmware packages"
dnf -y remove linux-firmware
dnf -y autoremove
echo "Remove previous kernels that preserved for rollbacks"
dnf -y remove -y $(dnf repoquery --installonly --latest-limit=-1 -q)
dnf -y clean all --enablerepo=\*;
echo "truncate any logs that have built up during the install"
find /var/log -type f -exec truncate --size=0 {} \;
echo "remove the install log"
rm -f /root/anaconda-ks.cfg /root/original-ks.cfg
echo "remove the contents of /tmp and /var/tmp"
rm -rf /tmp/* /var/tmp/*
echo "Force a new random seed to be generated"
rm -f /var/lib/systemd/random-seed
echo "Wipe netplan machine-id (DUID) so machines get unique ID generated on boot"
truncate -s 0 /etc/machine-id
echo "Clear the history so our install commands aren't there"
rm -f /root/.wget-hsts
export HISTSIZE=0
需要一些解释
echo "Installing cloud-init..."
dnf -y install cloud-init
# see https://bugs.launchpad.net/cloud-init/+bug/1712680
# and https://kb.vmware.com/s/article/71264
# Virtual Machine customized with cloud-init is set to DHCP after reboot
echo "manual_cache_clean: True" > /etc/cloud/cloud.cfg.d/99-manual.cfg
由于 vSphere 现在通过 VMware Tools 使用 cloud-init 来配置 centos8 客机网络,因此必须安装它。但是,如果您什么都不做,配置将在第一次重启时应用,一切都会正常。但在下次重启时,cloud-init 不会从 vSphere 接收任何新信息。在这种情况下,如果没有关于该怎么做信息的,cloud-init 将重新配置 VM 的网络接口以使用 DHCP,您将丢失静态配置。
由于这不是我们想要的行为,因此我们需要向 cloud-init 指定不要自动删除其缓存,因此要重用它在第一次重启和之后每次重启期间接收的配置信息。
为此,我们使用 manual_cache_clean: True
指令创建一个 /etc/cloud/cloud.cfg.d/99-manual.cfg
文件。
注意
这意味着,如果您需要通过 vSphere 客机自定义重新应用网络配置(在正常使用情况下,应该很少见),您将必须自己删除 cloud-init 缓存。
脚本的其余部分已加注释,不需要更多详细信息。
您可以查看 Bento 项目 以获取有关在此自动化过程部分中可以执行的操作的更多想法。
模板创建¶
现在该启动 Packer 并检查完全自动化的创建过程是否正常工作了。
只需在命令行中输入以下内容
./packer build -var-file=~/.vsphere-secrets.json rockylinux8/template.json
您可以快速转到 vSphere 并欣赏工作。
您将看到机器正在创建,启动,并且如果您启动控制台,您将看到命令的自动输入和安装过程。
在创建结束时,您将找到您的模板,准备在 vSphere 中使用。
部署部分¶
如果没有模板的自动部署部分,此文档将不完整。
为此,我们将使用一个简单的 Ansible 剧本,它使用 vmware_guest
模块。
我们为您提供的此剧本必须根据您的需求和做事方式进行调整。
---
- name: Deploy VM from template
hosts: localhost
gather_facts: no
vars_files:
- ./vars/credentials.yml
tasks:
- name: Clone the template
vmware_guest:
hostname: "{{ vmware_vcenter_hostname }}"
username: "{{ vmware_username }}"
password: "{{ vmware_password }}"
validate_certs: False
name: "{{ vm_name }}"
template: "{{ template_name }}"
datacenter: "{{ datacenter_name }}"
folder: "{{ storage_folder }}"
state: "{{ state }}"
cluster: "{{ cluster_name | default(omit,true) }}"
esxi_hostname: "{{ esxi_hostname | default(omit,true) }}"
wait_for_ip_address: no
annotation: "{{ comments | default('Deployed by Ansible') }}"
datastore: "{{ datastore_name | default(omit,true) }}"
networks:
- name: "{{ network_name }}"
ip: "{{ network_ip }}"
netmask: "{{ network_mask }}"
gateway: "{{ network_gateway }}"
device_type: "vmxnet3"
type: static
hardware:
memory_mb: "{{ memory_mb|int * 1024 }}"
num_cpu: "{{ num_cpu }}"
hotadd_cpu: True
hotadd_memory: True
customization:
domain: "{{ domain }}"
dns_servers: "{{ dns_servers.split(',') }}"
guest_id: "{{ guest_id }}"
register: deploy_vm
您可以将敏感数据存储在 ./vars/credentials.yml
中,您显然已事先使用 ansible-vault
对其进行了加密(尤其是在使用 git 进行工作时)。由于一切都使用变量,因此您可以轻松地使其满足您的需求。
如果您没有使用 Rundeck 或 Awx 之类的东西,则可以使用类似以下的命令行启动部署
ansible-playbook -i ./inventory/hosts -e '{"comments":"my comments","cluster_name":"CS_NAME","esxi_hostname":"ESX_NAME","state":"started","storage_folder":"PROD","datacenter_name":"DC_NAME}","datastore_name":"DS_NAME","template_name":"template-rockylinux8-0.0.1","vm_name":"test_vm","network_name":"net_prod","network_ip":"192.168.1.20","network_gateway":"192.168.1.254","network_mask":"255.255.255.0","memory_mb":"4","num_cpu":"2","domain":"rockylinux.lan","dns_servers":"192.168.1.254","guest_id":"centos8_64Guest"}' ./vmware/create_vm.yml --vault-password-file /etc/ansible/vault_pass.py
此时,您可以启动使用 Ansible 的虚拟机的最终配置。不要忘记更改 root 密码,保护 SSH,将新的 VM 注册到您的监控工具和 IT 库存中等等。
总结¶
正如我们所见,现在有完全自动化的 DevOps 解决方案来创建和部署 VM。
同时,这代表着时间的节省,尤其是在云或数据中心环境中。它还简化了整个公司(服务器和工作站)的标准合规性以及模板的轻松维护/演变。
其他参考¶
有关还涵盖使用 vSphere、Packer 和 Packer for vSphere 插件的最新技术部署 Rocky Linux 和其他操作系统的详细项目,请访问 此项目。
作者:Antoine Le Morvan
贡献者:Steven Spencer, Ryan Johnson, Pedro Garcia, Ganna Zhyrnova