openstackopenstack-novacloud-init

OpenStackCLI: Access `--property`'s from cloudinit script


Using the OpenStack CLI, I am creating server instances and need to pass custom properties (--property) into the init script referenced by the --user-data parameter.

So, my invocation looks something like this:

openstack server create myServer \
  ...other args...
  --user-data ./initScript.sh \
  --property "foo=bar"

In initScript.sh how do I get access to the foo property and it's value?


Solution

  • Elements defined with --property go into the meta element of the metadata. [source]

    If you run

    curl http://169.254.169.254/openstack/2018-08-27/meta_data.json
    

    You'll get

    {
       "hostname": "test.novalocal",
       …
       "meta": {
          "foo": "bar",
          …
       },
       …
    }
    

    And to parse out the value in a shell script, you can use for example:

    $ curl http://169.254.169.254/openstack/2018-08-27/meta_data.json \
        | jq -r '.meta.foo'
    bar
    

    See documentation for the Metadata Service for more information on the format and various methods available to retrieve this information.