Create droplet with User Data

Posted April 24, 2017 5.9k views
APIUbuntu 16.04

I getting started with DigitalOcean V2 API. When I tried create droplet with user data via API, server is created without executed my code. The same code when I did pasted in the browser and created droplet working properly.

This is my code:


sudo apt-get update
debconf-set-selections <<< "postfix postfix/mailname string"
debconf-set-selections <<< "postfix postfix/main_mailer_type string \'Internet Site\'"
apt-get install -y postfix
sudo postconf -e \'home_mailbox= Maildir/\'
sudo postconf -e \'virtual_alias_maps= hash:/etc/postfix/virtual\'

printf " root" > /etc/postfix/virtual
sudo postmap /etc/postfix/virtual
sudo systemctl restart postfix
sudo apt-get update
sudo ufw allow Postfix
echo \'export MAIL=~/Maildir\' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/
sudo apt-get install s-nail
echo \'init\' | mail -s \'init\' -Snorecord

What is the reason of my problem?

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.

Submit an Answer
2 answers

Hi @karolwojtyla820

I don’t see any closing ');, but I guess you just forgot to copy that.
I would guess it has something to do with the escaping of the '. Try changing all the \' to " and \'Internet Site\' to \"Internet Site\".
But from what I can remember, you need to #!/bin/bash on the first line - right now it’s on the second line.

Try running this command on the server to see what the user-data actually is:

curl -w "\n"


Instead of passing the data directly, I’d recommend passing it as a variable and using built-in functions such as file_get_contents.

For example:

$data = file_get_contents( '/path/to/' );

    [ $key ],

You’d then create a new file called and pass the path to file_get_contents.