Ask Your Question

Login in to account using cloud init script

asked 2016-08-31 15:23:50 -0500

Daniel Ramos gravatar image

updated 2016-09-03 09:24:28 -0500

I've been trying to automate the creation of a virtual machine using a cloud init script. I noted that this script is run using the machine root account. I wonder if it is possible to login in to the default machine account. For instance, I am currently using a trusty server image whose default login is ubuntu. I tried to do "sudo su ubuntu" in my cloud-init script, but unsuccessfully. It is weird because I don't get any error such as "No passwd entity for user". The script run this command (sudo su ubuntu), and right after - whoami, which returns "root". Is the default user created only after the cloud init is completed? Is too much exotic what I am trying to do?

My idea is to install some stuff (so far so good), then generate a ssh key adding it to the authorized keys. After that, I would snapshot the instance. The snapshot instance would be a "default" image to me start other instances afterwards. The tricky part is the ssh key generation. If I create a snapshot from an instance that can ssh to itself without password verification, when I launch other instances using that snapshot they will automatically be able to ssh to one another.

I was able to do this process without a cloud-init script, I wonder if I can do it by running a cloud-init script, in a more, automatic fashion (not having to login in to an image and deploy ssh stuff).

I am aware that I could use glance. When I said I was able to do it manually, I meant that I was using a process very similar. Instead of creating an image from scratch using a disk builder and uploading it to the cloud, I launched an instance, logged in to it, installed stuff, and configured ssh, then snapshot it (In my opinion it is a little easier than creating an image from scratch, moreover I've not been able to upload an image to the cloud I'm currently using, but that is another issue (I kinda got stuck and gave up :() - thus I decided to use this kind of approach).

edit retag flag offensive close merge delete


I have no idea how to execute commands as an unprivileged user with cloud-init, I'd use ansible for that. Maybe share your script in the question as maybe there are other approaches you could try with cloud-init.

smaffulli gravatar imagesmaffulli ( 2016-09-01 13:08:21 -0500 )edit

I improved my question. Please check it out.

Daniel Ramos gravatar imageDaniel Ramos ( 2016-09-02 12:28:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-02 17:27:45 -0500

smaffulli gravatar image

updated 2016-09-02 17:29:20 -0500

You should be able to automate most of that with a bash script. Even if the cloud-init process runs as root, you can create a user, install software, run ssh-keygen and move keys around and change ownership of files and directories... but that sounds to me very convoluted.

Maybe an ansible playbook would be simpler and it would also allow you to create the snapshot (things I believe you can't do with cloud-init).

The third and probably cleanest option, is to create a default image and upload it in Glance. Disk Image Builder is your friend here.

edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools



Asked: 2016-08-31 15:23:50 -0500

Seen: 597 times

Last updated: Sep 03 '16