When creating a vm via the vmware salt-cloud module. I need to be able to call an external script after the virtual machine has been created but before it is powered on, so that I can create a DHCP reservation and DNS entry for the vm, automation is clearly the goal here.
I have implemented a basic version which is working for me and I was happy to use that but I feel it would be helpful to get it back into the codebase from a selfish point of view to ease updating my salt install, and put it out there incase it was a feature that would be useful to other people. I would add additional exception handling around calling an external script.
Security consideration wise: You must trust the hook script obviously.
you can see the patch here at my fork :
https://github.com/conradjones/salt/commit/552729387e9b780cead6254b3cec3a9ef4bfeffc
vmware salt-cloud driver, latest devel branch
Create a virtual machine if "hook_script" is specified in the cloud provider or profile the vmware module will call the hook script (has to be python)
The hook script is passed a vim.VirtualMachine which it can do as it pleases.
I choose to implement this way to provide the most flexibility and not make the salt-cloud vmware module dependant on the hook scripts
Sample hook script "StaticReservation.py"
```from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
from BaseInstaller import InstallerConfig
from DNSInstaller import DNSInstaller
from DHCPInstaller import DHCPInstaller
from Config import Config
class VMWareHookScript:
def PrePowerOn(self, vm):
# Get mac address and network from VM
macaddress = None
for dev in vm.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
macaddress = dev.macAddress
network_name = dev.backing.network.name
subnet = "10.10.10.0"
# Setup DNS/DHCP wrapper objects
dhcp = DHCPInstaller()
dns = DNSInstaller()
# Create reservation in DHCP
ip = dhcp.create_dhcp_reservation(
subnet=subnet, host_name=vm.name, host_mac=macaddress)
# Create static A record in DNS
dns.create_a_record(host_name=vm.name, host_ip=ip,
domain_fqdn=options.dns_domain)
return True
Sample cloud.profile.conf
ubuntu-clone:
provider: vcenter
clonefrom: 'TEMPLATE'
cluster: 'Cluser'
ssh_username: someuser
password: notme
minion:
master: master
script: bootstrap-salt
script-args: git develop
hook_script: /srv/dns-dhcp/StaticReservation.py
```
Salt Version:
Salt: 2016.11.0-511-g5527293
Dependency Versions:
cffi: 1.5.2
cherrypy: Not Installed
dateutil: 2.4.2
gitdb: 2.0.0
gitpython: 2.1.1
ioflo: Not Installed
Jinja2: 2.8
libgit2: 0.24.0
libnacl: Not Installed
M2Crypto: Not Installed
Mako: 1.0.3
msgpack-pure: Not Installed
msgpack-python: 0.4.6
mysql-python: Not Installed
pycparser: 2.14
pycrypto: 2.6.1
pygit2: 0.24.0
Python: 2.7.12 (default, Nov 19 2016, 06:48:10)
python-gnupg: Not Installed
PyYAML: 3.11
PyZMQ: 15.2.0
RAET: Not Installed
smmap: 2.0.1
timelib: Not Installed
Tornado: 4.2.1
ZMQ: 4.1.4
System Versions:
dist: Ubuntu 16.04 xenial
machine: x86_64
release: 4.4.0-53-generic
system: Linux
version: Ubuntu 16.04 xenial
@conradjones this looks great. Would you be willing to submit as a PR?
If you want another set of eyes here I know @nmadhok has done a lot of work on this module. :)
I will submit a PR, I just need to add some exception handling around the call to the external script.
@conradjones how is it going? :)
Haven't used salt stack for some time
Intending to revisit it though.
On Tue, 3 Apr 2018, 13:54 Dmitry Miroshnichenko, notifications@github.com
wrote:
@conradjones https://github.com/conradjones how is it going? :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/saltstack/salt/issues/38337#issuecomment-378239284,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEMMfizD3SHCBaDevJSzRwLjBHme5sXWks5tk3DrgaJpZM4LQL-p
.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.
Most helpful comment
I will submit a PR, I just need to add some exception handling around the call to the external script.