← Back to team overview

cloud-init team mailing list archive

Re: cloud-init run command executed on every boot


On Fri, May 11, 2018 at 3:20 AM, Konstantinos Papadopoulos <
kostis.g.papadopoulos@xxxxxxxxx> wrote:

> Hello cloud-init experts. I have two basic question regarding cloud-init
> I have recently created an ubuntu server vm (using VBox) and I want to use
> the cloud-init service specifically the runcmd functionality. I am creating
> a file foo.yaml having the content below
> #cloud-config
> # run commands
> # default: none
> # 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
> # - runcmd only runs during the first boot
> # - 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 quote
> # any characters yaml would eat (':' can be problematic)
> runcmd:
>  - [ ls, -l, / ]
>  - [ sh, -xc, "echo $(date) ': hello world!'" ]
>  - [ sh, -c, echo "=========hello world'=========" ]
>  - ls -l /root
>  - [ wget, "http://slashdot.org";, -O, /tmp/index.html ]
> The question is. How can I configure my vm via cloud init so that the
> aforementioned runcmd is executed *everytime* the vm boots.
Hi Konstantinos,
a 'runcmd' will run once per instance.  So each newly deployed instance
will have those commands run once.
Think if runcmd as "instance initialization".

If you want something to run every boot, then you have 2 options:
a.) bootcmd .  that runs earlier in the boot process, and runs every boot.
it looks same as 'runcmd' in format.
b.) put scripts into /var/lib/cloud/scripts/per-boot/

Here is an example http://paste.ubuntu.com/p/w8QQjN4dPC/ (and attached) of
using write_files to write files into those directories.
The per-once script will only run once (you can remove the file
/root/my-per-once.log and reboot to verify).
The per-boot script will run every boot.

> The second question is how will I use this foo.yml so that the this
> runcmd  is executed only once when the the vm is created. The vm will be
> created through openstack and Vbox.

I think runcmd is probably what you want.  If you deploy a new instance
from this image, you'd probably want to run your scripts again.
If that is not the case, and what you really do want is once *ever*, then
you can put things in /var/lib/cloud/scripts/per-once/ .  That directory
will have 'run-parts' run on it by cloud-init only once ever (based on
presence of /var/lib/cloud/sem/config_scripts_per_once.once).

Attachment: my-write-files
Description: Binary data

Follow ups