pulumi-templates/linode-ansible/__main__.py

44 lines
1.3 KiB
Python
Raw Normal View History

"""A Linode Python Pulumi program"""
import pulumi
import pulumi_linode
import pulumi_command as command
import yaml
from dotmap import DotMap
config = DotMap(yaml.safe_load(open("input.yaml").read()))
# Create a Linode resource (Linode Instance)
instance = pulumi_linode.Instance(config.vm.name,
type=config.linode.type,
region=config.linode.region,
image=config.linode.image,
label=config.vm.name,
root_pass=config.vm.root_password,
authorized_keys=config.vm.sshkeys,
opts=pulumi.ResourceOptions(
custom_timeouts=pulumi.CustomTimeouts(create="2m")
)
)
# Creating the inventory file
inventory = command.local.Command("a-inventory",
create = instance.ip_address.apply(
lambda ipaddr: f"echo '{config.vm.name} ansible_host={ipaddr} ansible_user=root' >./inventory"
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ instance ])
)
# Try the deployment with ansible
# Applying the command
execute_ansible = command.local.Command("a-deploy",
create = f"ansible-playbook {config.vm.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)