For contextualization, we distinguish between so called meta-data and user-data.
The meta-data is provided by the cloud infrastructure and is not modifiable by the user.
For example, meta-data includes the instance ID as issued by the cloud infrastructure and the virtual machines assigned network parameters.
The user-data is provided by the user on creation of the virtual machine.
User data is a blob of data that the user can specify when they launch an instance. The instance can access this data through the metadata service or config drive. Commonly used to pass a shell script that the instance runs on boot.
Nova itself needs to pass information to the instance via its internal implementation of the metadata system. Such information includes the network configuration for the instance, as well as the requested hostname for the instance. This happens by default and requires no configuration by the user or deployer.
$ curl http://169.254.169.254/2009-04-04/meta-data
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
local-hostname
local-ipv4
placement/
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
$ curl http://169.254.169.254/2009-04-04/meta-data/hostname
amu-fenic01.cppm-cloud.in2p3.fr
$ curl http://169.254.169.254/2009-04-04/meta-data/local-ipv4
192.168.1.17
$ curl http://169.254.169.254/2009-04-04/meta-data/public-ipv4
134.158.21.25
You can place user data in a local file and pass it through the --user-data
$ openstack server create --image ubuntu-cloudimage --flavor 1 --user-data mydata.file VM_INSTANCE
Ex:
$ echo -n "Hello World ..... " `date` > my_user_data.txt
$ openstack server create --flavor m1.medium --image Fedora-27
--nic net-id=$OS_NET --user-data my_user_data.txt
--security-group default --key-name jhondoe-key my_test2
The instance can retrieve user-data by querying the metadata service through either the OpenStack metadata API or the EC2 compatibility API:
Ex:
$ curl http://169.254.169.254/2009-04-04/user-data
Hello World ..... Wed Apr 11 12:10:13 CEST 2018
You can also run a bash script using
$ cat test.sh
#!/bin/bash
useradd a
useradd b
useradd c
yum -y install httpd
yum -y update
$ openstack server create --flavor m1.medium --image Fedora-27
--nic net-id=$OS_NET --user-data test.sh --security-group default
--key-name jhondoe-key my_test2
OpenStack enables automatic setup of new instances using Cloud-init. Cloud-init runs on first boot of every new instance and initializes it according to a provided script or config file.
Using the cloud-config syntax many different actions are possible. Add user accounts, configure mount points, initialize Puppet and much more is posible.
For example, the same Apache initialization as above can be done with the following cloud-config statements:
$ cat cloudinit_centos7.cloud
#cloud-config
packages:
httpd
runcmd:
[ useradd, "a" ]
[ useradd, "b" ]
[ useradd, "c" ]
After that do you use the --user-data capability
$ openstack server create --flavor m1.medium --image Fedora-27
--nic net-id=$OS_NET --user-data cloudinit_centos7.cloud
--security-group default --key-name jhondoe-key my_test2
To get a deeper understanding cloud-init Documentation