diff --git a/templates/common/vsphere/files/vsphere-hostname.yaml b/templates/common/vsphere/files/vsphere-hostname.yaml new file mode 100644 index 0000000000..2107d7b704 --- /dev/null +++ b/templates/common/vsphere/files/vsphere-hostname.yaml @@ -0,0 +1,14 @@ +filesystem: "root" +mode: 0755 +path: "/usr/local/bin/vsphere-hostname.sh" +contents: + inline: | + #!/usr/bin/env bash + set -e + + if [ $(hostname -s) = "localhost" ]; then + if hostname=$(/bin/vmtoolsd --cmd 'info-get guestinfo.hostname'); then + /usr/bin/hostnamectl --transient --static set-hostname ${hostname} + fi + fi + diff --git a/templates/common/vsphere/units/vsphere-hostname.yaml b/templates/common/vsphere/units/vsphere-hostname.yaml new file mode 100644 index 0000000000..4c57b2dd6f --- /dev/null +++ b/templates/common/vsphere/units/vsphere-hostname.yaml @@ -0,0 +1,16 @@ +name: "vsphere-hostname.service" +enabled: true +contents: | + [Unit] + Description=vSphere hostname + After=vmtoolsd.service + Before=kubelet.service + + [Service] + ExecStart=/usr/local/bin/vsphere-hostname.sh + Restart=on-failure + RestartSec=15 + + [Install] + WantedBy=multi-user.target +