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?
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.
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.
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 ").
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With