pulumi-templates/linode-ansible/__main__.py

56 lines
1.7 KiB
Python
Raw Normal View History

"""A Linode Python Pulumi program"""
import pulumi
import pulumi_linode
import pulumi_command as command
import pulumi_cloudflare as cloudflare
import yaml
from dotmap import DotMap
config = DotMap(yaml.safe_load(open("input.yaml").read()))
hostname = pulumi.Config().require("hostname")
domainname = pulumi.Config().require("domainname")
# Create a Linode resource (Linode Instance)
instance = pulumi_linode.Instance(hostname,
type=pulumi.Config().require("linodeType"),
region=pulumi.Config().require("linodeRegion"),
image=pulumi.Config().require("linodeImage"),
label=hostname,
root_pass=config.rootpassword,
authorized_keys=config.sshkeys,
opts=pulumi.ResourceOptions(
custom_timeouts=pulumi.CustomTimeouts(create="2m")
)
)
zone = cloudflare.get_zone(name=domainname)
dnsrecord = cloudflare.Record(hostname,
name=hostname,
zone_id=zone.id,
type="A",
content=instance.ip_address,
comment=f"Pulumi-generated linode"
)
# Creating the inventory file
inventory = command.local.Command("a-inventory",
create = instance.ip_address.apply(
lambda ipaddr: f"echo '{hostname} ansible_host={ipaddr} ansible_user=root hostname={hostname}.{domainname}' >./inventory"
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ instance ])
)
for playbook in config.playbooks:
shortname = playbook.split('.')[0]
# Applying the command
command.local.Command(f"a-deploy-{shortname}",
create = f"ansible-playbook {playbook}",
delete = "rm -f ./ansible.log",
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
)
# Export the Instance label of the instance
pulumi.export('ip_address', instance.ip_address)