digitalocean / doctl

The official command line interface for the DigitalOcean API.
https://docs.digitalocean.com/reference/doctl/
Apache License 2.0
3.26k stars 397 forks source link

Getting a list of my Droplets into one file? #288

Closed AFvloginov closed 6 years ago

AFvloginov commented 6 years ago

Hi All,

Does anyone know how to get a list of all VM droplets into one file? Ideally, it would be great to get all VMs with the following details:

Thanks in advance.

xmudrii commented 6 years ago

Hi there,

You can use the output redirection to forward output to the file, for example:

doctl some-command > doctl-output.txt

To filter down the output and choose only needed columns, you can use the --format flag. For example, the doctl compute droplet list --format="Name,Memory,Disk,Status" command would return something along:

Name                    Memory    Disk    Status
ubuntu-512mb-nyc3-01    512       20      active
ubuntu-512mb-nyc3-02    512       20      active

Append > output.txt to the end of the command, to get the output.txt file with same content as the above output.

You can also hide headers using the --no-header flag:

doctl compute droplet list --format="Name,Memory,Disk,Status" --no-header
ubuntu-512mb-nyc3-01    512    20    active
ubuntu-512mb-nyc3-02    512    20    active

This is the plain-text output and therefore, it could be harder to parse it. You can also output the command's result in JSON format, using the --output flag:

doctl compute droplet list --output=json
[                        
  {                                                                                                                                                                             
    "id": 78395000,              
    "name": "ubuntu-512mb-nyc3-01",   
    "memory": 512,                      
    "vcpus": 1,                   
    "disk": 20,              
    "region": {   
      "slug": "nyc3",                            
      "name": "New York 3",
...

Note that --format and --output=json flags doesn't work together -- with JSON format you'll always get all fields. You can use some tool such as jq to additionally parse it.

Beside list command, there's get command, which support templates. However, it requires you to explicitly provide Droplet's ID.

For more details on templating and formatting output, check out our Community tutorial, How To Use Doctl, the Official DigitalOcean Command-Line Client. If you have any questions, feel free to ping me here and I'll try to help you as much as I can.