Getting ec2 metadata to rancher host labels on rancheros


So here’s my scenario. I’d like to set some host labels on my rancher agent that is running on rancheros. These labels should be based off information from the ec2 metadata service as I’m running on AWS. I am very stuck as to how to make this happen in RancherOS. Here’s what I’ve tried:

  1. Use write_files to create a script that uses ros config to set rancher.environment.CATTLE_HOST_LABELS to what I need, then referencing said variable in my cloud-config like this:
      …other docker stuff…

This doesn’t seem to work. I was assuming the ros env command was applied to services that you use cloud-config to run, but maybe not?

  1. Instead of using cloud-config, just set my user data to a simple bash script that runs the rancher agent container directly. In this scenario the container never launched.

Anyone got any better ideas on how I might achieve this?


I was able to follow the docs to get an agent running into Rancher and using the environment, but am still looking into doing it with the cloud-config.

The main hurdle that I’m not sure about is how to use the sudo ros env command while booting.

Here’s what I have tried:

After RancherOS is launched:

sudo ros set rancher.environment.CATTLE_HOST_LABELS 'foo=bar'
sudo ros env docker run -e CATTLE_HOST_LABELS --d --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:v0.8.2  http://<rancher-server-ip>:8080/v1/projects/1a5/scripts/<registrationToken>

I believe having the equivalent in your cloud-config would set the environment.

     CATTLE_HOST_LABELS: foo=bar #this is where you'd need to figure out how to set your metadata items

The part I’m still looking into is how to call the sudo ros env command to start your service that would use the environment variable that you just set.

Thanks for the update Denise - it looks like you ran through pretty much the same scenario I did and ran into the same problem. :slightly_smiling:


Any news on this?

If I try to do something like below, I get nothing:

CATTLE_HOST_LABELS: instance-type=$(wget -qO-

I’m still stuck on how to set ec2 metadata to rancher-os
Any progress on this topic ?

I’m doing something like this:

sudo docker run -d -e CATTLE_HOST_LABELS="az=`wget -qO-`&cluster_name=${cluster_name}&host_type=${host_type}&HOSTID=`wget -qO-`" --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:${agent_ver} https://${rancher_mgr_url}/v1/scripts/${env_hash}

I know this is an old thread, being that I found it searching for a solution to the same problem, maybe this reply will help others…

Scenario: using cloud-config to bootstrap rancher-agent instances, on rancheros

The fix: Use curl (wget doesn’t appear to be available on rancher-agent container)

   priviledged: true
   - /var/run/docker.sock:/var/run/docker.sock
   image: rancher/agent
    CATTLE_AGENT_IP: $private_ipv4
   command: ${rancher_registration_token.env-token.registration_url}
1 Like