Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use variables in cloud-config

When using cloud init's #cloud-config to create configuration files, how would I go about using variables to populate values?

In my specific case I'd like to autostart EC2 instances as preconfigured salt minions. Example of salt minion cloud config

Say I'd like to get the specific EC2 instances id and set that as the salt minion's id.

How would I go about it setting the value dynamically for each instance?

like image 257
charliemurder Avatar asked May 14 '13 10:05

charliemurder


People also ask

How do I set environment variables in cloud run?

To set, update, or remove environment variables of an existing service, use the gcloud run services update command. You can use any of the following flags, as needed: --set-env-vars. --update-env-vars.

What is cloud config?

Cloud configuration is the process of setting hardware and software details for elements of a cloud environment to ensure that they can interoperate and communicate.

How do you set a variable in terminal?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


2 Answers

in boot command bootcmd can have environment variable $INSTANCE_ID, you can save it for later usage. see http://cloudinit.readthedocs.org/en/latest/topics/examples.html#run-commands-on-first-boot

I for example do like below

#cloud-config
bootcmd:
  - echo $INSTANCE_ID > /hello.txt
like image 105
Larry Cai Avatar answered Oct 06 '22 07:10

Larry Cai


The closest I've seen to configurable variables is [Instance Metadata].(https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html#)

It says you can use:

  • userdata provided at instance creation

You can use data created in /run/cloud-init/instance-data.json.

You can use import this instance data using Jinja templates in your YAML cloud-config to pull in this data:

## template: jinja
#cloud-config
runcmd:
    - echo 'EC2 public hostname allocated to instance: {{
      ds.meta_data.public_hostname }}' > /tmp/instance_metadata
    - echo 'EC2 availability zone: {{ v1.availability_zone }}' >>
      /tmp/instance_metadata
    - curl -X POST -d '{"hostname": "{{ds.meta_data.public_hostname }}",
      "availability-zone": "{{ v1.availability_zone }}"}'
      https://example.com

But I'm not exactly sure how you create the /run/cloud-init/instance-data.json file.

This CoreOS issue suggests that if you put variables into /etc/environment then you can use those.

I know for example that there are a few variables used such as $MIRROR $RELEASE, $INSTANCE_ID for the phone_home module.

like image 37
icc97 Avatar answered Oct 06 '22 08:10

icc97