How do I list saved images using the Linode Python API bindings?

Linode Staff

I'm trying to deploy Linodes from a Python script using a saved image. How do I list images using the Linode Python API bindings?

2 Replies

You can list images on your account from a Python script using code like the following:

from linode_api4 import LinodeClient

token = "$TOKEN"
client = LinodeClient(token)

list_of_images=client.images()

Just remember to replace $TOKEN with your actual API token.

Images created by you and stored on your account are named using the convention private/$IMAGE, where $IMAGE is the name of your saved image. For example, to deploy a Linode using an image called testimage, you could use code like the following:

new_linode, password = client.linode.instance_create(
               "g6-standard-2",
               "us-east",
               image="private/testimage")

If the name of the image you wish to use is stored in an array, as in the first example above, you can reference it like this:

new_linode, password = client.linode.instance_create(
               "g6-standard-2",
               "us-east",
               image=list_of_images[0])

It's worth noting that my python is a bit rusty, so please excuse any basic syntax/formatting errors in my examples.

I just wanted to provide some clarity regarding how to list your private images so you can reference them by their id.

Once you import the LinodeClient and set your token, etc., you can list your private images using a for loop, like this:

from linode_api4 import LinodeClient
token=$TOKEN

client = LinodeClient(token)
list_of_images=client.images()

for my_image in list_of_images:
    if my_image.is_public == False:
        print(my_image.label, my_image.id)

This will list your private images and their associated id.

You'll want the id's, so you can reference them in your client.linode.instance_create command.

First, list your regions (if needed).

available_regions = client.regions()

for region in available_regions:
    print(region)

This lists out:

Region: ap-west
Region: ca-central
Region: ap-southeast
Region: us-central
Region: us-west
Region: us-southeast
Region: us-east
Region: eu-west
Region: ap-south
Region: eu-central
Region: ap-northeast

Choose your region. I'm picking us-east.

chosen_region = available_regions[6]

Create your Linode with the plan of your choice, using the private image id:

new_linode, password = client.linode.instance_create('g6-standard-2',
    chosen_region,
    image='private/14784389')

This creates a Linode with a randomly generated root password.

Print out the results so you can access your new server.

print("ssh root@{} - {}".format(new_linode.ipv4[0], password))

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct