koriner
By:
koriner

Help with cloud init syntax for runcmd

October 22, 2014 8.5k views

I'm just getting started with cloud-config etc for setting up a droplet, and I'm just running into a slight issue with running commands.

I'm trying to run some commands which have characters that upset the YAML parser, for instance:

runcmd:
  - \curl -sSL https://get.rvm.io | bash -s stable

The cloud init logs are giving me an error for runcmd, I'm assuming because it's incorrectly parsing characters like ":" etc.

What is the correct way to specify this kind of command? Can I just put quotes around part of it, or should I escape the special characters somehow?

Cheers!

1 Answer

The documentation is a bit lacking, but from the comments on the example in the upstream docs you learn:

# runcmd contains a list of either lists or a string
# each item will be executed in order at rc.local like level with
# output to the console
# - if the item is a list, the items will be properly executed as if
#   passed to execve(3) (with the first arg as the command).
# - if the item is a string, it will be simply written to the file and
#   will be interpreted by 'sh'
#
# Note, that the list has to be proper yaml, so you have to escape
# any characters yaml would eat (':' can be problematic)

So wrapping everything in single quotes seems like it should do the trick:

#cloud-config
runcmd:
 - '\curl -sSL https://get.rvm.io | bash -s stable'

The resulting script can be found for debugging purposes in /var/lib/cloud/instance/scripts/runcmd

  • Thanks for that - using quotes around it all works great. I do have another question while I'm here.

    Is it possible to run commands here as another user? I'm assuming these are all run as root, but for some of my setup I want to run it as a new user which is also being created with the cloud config script.

    The command above is a good example, I don't want to install rvm as root, I want to install it for my user "admin" which gets created during the setup.

    Perhaps I can just use su to accomplish this? e.g.

    runcmd:
     - 'su -c "\curl -sSL https://get.rvm.io | bash -s stable" admin'
    

    I haven't tested this yet but just thought I'd post here as someone might know!

Have another answer? Share your knowledge.