Question

Droplet create - user data not executed

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”, “ssh_keys”: [“‘$SSH_PUBLIC_KEY_DO_FORMAT’”], “user_data”: "`#cloud-config runcmd:

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

Thanks for the help


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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