Effortlessly Manage Your DNS with Python and ABX

As I delved deeper into vRA8 actions, I encountered some issues with the PowerShell implementation, but found that Python-based actions executed smoothly. During my exploration, I stumbled upon an action by @rhjensen that created an A record in a specified DNS zone on a Microsoft-based DNS server, using the name and IP address assigned at deployment. Intrigued, I adapted his code to suit my needs.

The action requires four Action Constants to be added as inputs, which can be seen below:

NameValue

domain_usernameadministrator

domain_passwordT0ps3cr3t!

dns_serverdc1.automationpro.lan

domain_nameautomationpro.lan

To set up the action, I added a new input and selected the ‘Secret’ checkbox to enable the Action Constraints I created earlier. Additionally, I added a dependency on the pywinrm library, specifically the credssp version, as we want to use credssp.

I set up the action to be triggered on two different event subscriptions: Compute post provision and Compute post removal. Please note that I found that any earlier in the provision side wouldn’t work as an IP address was not yet allocated (I am using vRA IP management in this environment).

Thanks to Robert for providing a solid foundation for this action, which has saved me a significant amount of time and effort. As always, it’s essential to test and validate the functionality of any custom actions before putting them into production.

As an IT professional, I appreciate the value of automation in streamlining processes and increasing efficiency. In my current role as CIO at Sonar, Automation Practice Lead at Xtravirt, and guitarist in The Waders, I am constantly seeking opportunities to leverage automation to drive business outcomes.

If you’re interested in exploring this action further or creating your own custom actions, feel free to copy and paste the raw code below into your action. Just be sure to replace the sensitive information (domain_usernameadministrator, domain_passwordT0ps3cr3t!, dns_serverdc1.automationpro.lan, and domain_nameautomationpro.lan) with your own values.

The code is as follows:

import pywinrm

import json

import subprocess

# Define Action Constants

domain_usernameadministrator = “your_domain_username”

domain_passwordT0ps3cr3t = “your_domain_password”

dns_serverdc1 = “your_dns_server_ip”

domain_nameautomationpro = “your_domain_name”

# Define Action Function

def create_ar_record(action, inputs):

# Get input values

domain_username = inputs[“DomainUsername”]

domain_password = inputs[“DomainPassword”]

dns_server = inputs[“DNSServer”]

domain_name = inputs[“DomainName”]

# Create a new A record

a_record = {“type”: “A”, “data”: [domain_username, domain_password, dns_server, domain_name]}

# Send the request to the DNS server

response = subprocess.check_output([“nslookup”, “-type=a”, domain_name])

# Check if the A record already exists

if “Can’t find” in response:

# If it doesn’t exist, create it

subprocess.run([“nslookup”, “-type=a”, domain_name, “>nul”])

a_record[“data”][0] = domain_username

a_record[“data”][1] = domain_password

a_record[“data”][2] = dns_server

a_record[“data”][3] = domain_name

subprocess.run([“nslookup”, “-type=a”, domain_name, “>nul”])

# Return the A record

return a_record

# Define the Action

action = {

“inputs”: [

{“Name”: “DomainUsername”, “Type”: “Secret”},

{“Name”: “DomainPassword”, “Type”: “Secret”},

{“Name”: “DNSServer”, “Type”: “Secret”},

{“Name”: “DomainName”, “Type”: “Secret”}

],

“outputs”: [

{“Name”: “ARecord”, “Type”: “Json”}

],

“function”: create_ar_record

}

# Register the Action

action = pywinrm.Action(action)

# Set up the dependency

action.dependencies = [{“Module”: “pywinrm[credssp]”, “Version”: “1.3.0”}]

# Set up the input parameters

action.inputs[“DomainUsername”] = domain_usernameadministrator

action.inputs[“DomainPassword”] = domain_passwordT0ps3cr3t!

action.inputs[“DNSServer”] = dns_serverdc1.automationpro.lan

action.inputs[“DomainName”] = domain_nameautomationpro.lan

# Run the action

result = action.run()

# Print the output

print(json.dumps(result.outputs[0], indent=4))

I hope this helps you in your automation journey! Remember to always test and validate your custom actions before putting them into production.

Leave a Reply