I'm new to Pulumi and learning myself pulumi inste...
# python
b
I'm new to Pulumi and learning myself pulumi instead of terraform and I'm trying to create a simple script to provision a new virtual machine (VM) in vSphere every time I run 'pulumi up'. However, it seems that my current script updates the existing VM instead of creating a new one.
Copy code
import pulumi
import pulumi_vsphere as vsphere

# Names of your resources in vSphere
datastore_name = 'roels-bart'
network_name = 'VM Network'
resource_pool_name = 'test'
datacenter_name = 'StudentDC'
template_name = 'CleanUbuntu'

# Lookup the Datacenter by name
datacenter = vsphere.get_datacenter(name=datacenter_name)

# Lookup the Datastore by name
datastore = vsphere.get_datastore(name="roels-bart", datacenter_id=datacenter.id)

# Lookup network by name
network = vsphere.get_network(name="VM Network", datacenter_id=datacenter.id)

# Default parent resource pool
pool = vsphere.get_resource_pool(name="Resources", datacenter_id=datacenter.id)

# Lookup template by name
template = vsphere.get_virtual_machine(name="CleanUbuntu", datacenter_id=datacenter.id)

# Create a when this script is runned
virtual_machine = vsphere.VirtualMachine("pulumi-vm",
    name='pulumi-vm',
    datastore_id=datastore.id,
    resource_pool_id=pool.id,
    num_cpus=1,
    memory=1024,
    guest_id='ubuntu64Guest',
    scsi_type='pvscsi',
    network_interfaces=[{
        'network_id': network.id,
        'adapter_type': 'vmxnet3',
    }],
    clone={
        'template_uuid': template.id,
    },
    # Assing disk
    disks=[
        {
            'label': 'disk0',
            'size': 20,
            'eagerly_scrub': False,
            'thin_provisioned': True,
        },
    ],
    # Customise the VM's configuration

)



# Export the virtual machine's properties
pulumi.export('vm_name', virtual_machine.name)
pulumi.export('vm_id', virtual_machine.id)
Can someone help me modify this script to ensure that a new VM is created with every 'pulumi up' run, rather than updating the existing one? Also, I'm curious if there's a recommended way to run Pulumi scripts from another location, like a Flask application. I'm thinking of a scenario where users can input parameters through a web interface to customize the VM creation process. Any suggestions or best practices for achieving this would be greatly appreciated. Thanks in advance!
d
The code you write is reflective of your overall deployment, it doesn't create new resources unless they don't yet exist. Something you can do is use multiple stacks, or load in data from another source to define multiple VM resources in pulumi
Regarding integrating with flask, it's with looking to the Pulumi automation api: https://www.pulumi.com/docs/using-pulumi/automation-api/
b
Thanks for the reply this is already helping me tremendously! I started experimenting with the pulumi API here for a while but I'm not good with what to enter at "stack.workspace.install_plugin". In my case I am using vsphere should I fill in "stack.workspace.install_plugin("vsphere", "x.x.x.x")" there or?
l
@brash-nightfall-51586 the program you posted initially is a pure infrastructure program. You typically run this with the
pulumi
CLI. The Automation API that @dry-keyboard-94795 refers to allows you to wrap a full Pulumi project and run the CLI commands via an API. In the simplest setup, you can refer to a local program, meaning it is a local folder containing a workable infrastructure program. In such case there is no need to run
install_plugin
as this will be done automatically. A step up is if you combine your infrastructure as code program with the automation API code in a single program. From the automation API code you then use an "inline program". You then have to use "install_plugin" to install the plugin binaries. For VSphere, you indeed use
stack.workspace.install_plugin("vsphere", "4.9.1")
. The version should point to an existing version for the VSphere package (see Pulumi Registry) and should match the version of the Pulumi Python package for VSphere you have in your
requirements.txt
. Hope this clarifies a bit.
@brash-nightfall-51586 if
roels-bart
is your name, that sounds very Flemish to me. If you are from the vicinity, I want to inform you that a few of us from the Pulumi team will be at CfgMgmtCamp in Ghent from 5 to 7 Februari. On Wednesday 7 February, we have an open devroom. Feel free to come over with all of your questions, but don't forget to register on the CfgMgmtCamp website. https://cfgmgmtcamp.eu/ghent2024/
b
Hi @limited-rainbow-51650! Thanks for the reply after some searching and reading the docs I was able to create a fully working script! I am indeed a as you suggested Flemish. Am currently a final year student at Howest MCT with graduation profile infrastructure engineering. Am working on a research project "How to set up a one-click on-premise deployment pipeline for a full stack application using Infrastructure as Code and Configuration as code?". Where I am including pulumi, ansible etc. I will definitely keep CfgMgmtCamp in mind! Can always come in handy 🙂
g
I’m also new to Pulumi & also from a TF background. The conclusion I’m coming to is that Pulumi (in TF terms) is effectively using a coding language (Python) to create HCL that then gets applied: the output of Python is Pulumi configuration. Its not a Python script that also executes against the AWS APIs (like boto3) as it executes. You dont run the python code produced - pulumi does that & then uses the resulting config. You can call non-Pulumi python from Pulumi (as part of building the Pulumi elements/config, but not the other way round. I’ve yet to get into if having a standard python script that calls Pulumi outputs, but I assume thats possible. Hopefully someone will correct me if I’m wrong…
d
@green-alligator-4238 yep, you've got it. And yes, you can access outputs from other apps. I used the json formatted version of
pulumi output
to load it into other tooling
g
Thanks @dry-keyboard-94795 So, @brash-nightfall-51586, to answer your question, you’re running a TF apply every time you run pulumi up - you’ll get the same single deployment. Its not like boto3 when you’ll get (or try to get) a new deployment (say with a different VM name). Not that I’ve looked for it yet, but there may be a count/for_each equivalent. I’d be interested in any thoughts on building a Pulumi ‘engine’ to churn out VMs, which is possibly what you’re looking for. Interesting challenge…