Question

Droplet create - user data not executed

Posted April 30, 2020 187 views
DigitalOcean

I’m trying to create a droplet and adding a code (in this case a hardcoded ‘saraza’) to a file for later purposes. This code will change from droplet to droplet so I needed parametrized. But when I execute this, no file is created in my droplet. I’ve seen another questions that said that this is the way to do this.

curl -v -X POST “https://api.digitalocean.com/v2/droplets” \
-d’{“name”:“My-Droplet”,“region”:“nyc3”,“size”:“s-1vcpu-1gb”,“image”:“ubuntu-18-04-x64”, “sshkeys”: [“’$SSHPUBLICKEYDOFORMAT’”], “userdata”: “`#cloud-config
runcmd:

  • echo saraza > ~/file.txt`”}’ \ -H “Authorization: Bearer $DOAPITOKEN” \ -H “Content-Type: application/json”)

Thanks for the help

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hello, @dseidler

I will recommend you to check our official docs for the User Data:

https://www.digitalocean.com/docs/droplets/resources/metadata/#about-user-data

As shown in the example in the article you can simply create a bash script that will execute the command you want after the droplet creation.

curl -X POST "https://api.digitalocean.com/v2/droplets" \
      -d’{“name”:“My-Droplet”,“region”:“nyc3”,“size”:“s-1vcpu-1gb”,“image”:“ubuntu-18-04-x64”,"user_data":
"#!/bin/bash

echo saraza > ~/file.txt
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json"

You can try with the example above and see if it works for you.

Note: You can also check the output logs and see what happened when the startup script tried to run the user-data

The log is located here:

/var/log/cloud-init-output.log

Let me know how it goes.

Regards,
Alex

Submit an Answer