Getting the cattle registration env vars locally on the rancher server

On the rancher server I’d like to pull the env vars for host registration so I can automate with another system adding additional hosts without a human.

Its actually very similar to Automating installation of rancher/server and ranger/agent (my system will be a RightScale Server Array instead of an AWS AS group).

I’d just like to get it locally on the rancher server, effectively retrieving:


export CATTLE_REGISTRATION_ACCESS_KEY="registrationToken"
export CATTLE_REGISTRATION_SECRET_KEY="B0B1DF27C2EA977B3B0C:1437886800000:KywjS1FC1Qh6NpiHnUH2hr6aEjw"
export CATTLE_URL="http://myrancherserver:8080/v1"

I assume there will need to be a command without auth to pull it out of rancher or its DB or potentially we could have rancher server dump into a file somewhere, say /var/lib/rancher/

If its not possible yet in some form, certainly a feature request from me :smile:

Ok this post gave me the hint I needed Is there a way to create a digital ocean host programmatically?. If there is a better method, I’m all ears!

#!/usr/bin/env python

import os
import cattle
import requests


client = cattle.Client(url=os.environ['RANCHER_URL'] + '/v' + str(RANCHER_API_VERSION),

registration_url = client.list_registrationToken()[0]['registrationUrl']
registration_file = '/var/spool/rancher/'

print('GET: ' + registration_url)
r = requests.get(registration_url)

print('Writing to ' + registration_file)
if not os.path.exists('/var/spool/rancher'):
f = open(registration_file, 'w')

os.chmod(registration_file, 0600)
os.chown(registration_file, 0, 0)

This should pretty much work, with a couple caveats… It is not guaranteed that there is a token to get, the UI does get and the creates one if there isn’t (and waits for it to be active). They are also good for a limited/configurable amount of time, currently a year, but this should probably much lower.

Realistically your script should be fine though. In the nearish future it should be easy to use the rancher CLI to create one on demand.

Ok cool, thanks @vincent! It is working nicely.