1

I'm quite new to cloud init. I started documenting few days ago, but all what I found is related to KVM or cloud enviroments, and they does not rapresent my need.

I want to use cloud init in an bare metal environment (I'm using centOS). Let's say that I do not want to use cloud init for the the network configuration and all the other modules that are provided.

My need is to use cloud-init to launch a command when the server reboots... Suppose that I want to launch a touch commad:

  • touch /root/hello.txt

How do I have to configure cloud-init in order to achieve this?

I see that there are some files and folders inside /etc/cloud/:

  • /etc/cloud/cloud.cfg
  • /etc/cloud/cloud.cfg.d/

I see that inside the files /etc/cloud/cloud.cfg there are all the boot stages...

Can you explain me step-by-step what I have to do in order to have my cloud-init that execute the command touch /root/hello.txt at the next reboot (and get rid of all the other operations that are done).

I suppose that I have to set Datasource: NoCloud, and crate meta-data and user-data, but I cannot figure out how to do that.

Thanks in advance!

1 Answers1

1

Let me start by explaining how to use cloud-init on bare metal.

You do not have to explicitly configure cloud-init to use the NoCloud datasource. It will detect this on its own as long as you provide a vfat or iso9660 filesystem labeled cidata that contains files named meta-data and user-data. For bare metal, you could put the filesystem on a USB drive. For example:

cat > user-data <<EOF
#cloud-config
runcmd:
  - touch /root/hello.txt
EOF
touch meta-data
genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data

Then write seed.iso directly to a USB drive. For example, if your USB drive is /dev/sdc, run:

dd if=seed.iso of=/dev/sdc

Insert the USB drive into your new machine and boot the cloud-init image. It should find and use the configuration you put into user-data, and create an empty /root/hello.txt on the first boot. Note that the runcmd module only runs these commands once per instance.

It sounds like your question is actually about running a script on every boot. If you want to do this with cloud-init, see How do I make cloud-init startup scripts run every time my EC2 instance boots for details.

The way you've written your question also makes me suspect that you're not using cloud-init for initial setup, and all you're looking for is a way to run a command on every reboot for an existing install. If that's the case, then cloud-init probably isn't the right solution for you. You should just set up a systemd service of type oneshot.

satwell
  • 222
  • 2
  • 7