Login in to account using cloud init script
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).
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.
I improved my question. Please check it out.