SmoothLinux Blog

Salt-Cloud Setting Hostname Appending IP Address

15 November, 2015 | salt

I have a environment requirement to deploy AWS instances and to keep our instance naming schema. Which is instance_name and the last two octets of the IP address appended to the end of the instance name. It would look like something like this: example000000. I'm currently trying to use salt for my deployments going forward so this bring some new challenges.

***Disclaimer I have only been using salt for a couple of weeks so I'm sure there are better way to do this then what I'm currently doing***

If you know please share it with me.

I ran into a few issues during this process:

1) I could not get the script in salt in 2015.8.1 to work with a AWS Ubuntu instances out of the box. So to get over this hump I just create my own script.

2) I hate having to use anything other then a basic AIM deployment of the OS to stand up a instance. For my setup I had to add a script to my AIM instance that I'm using in my salt-cloud profile.

3) When the script sets the hostname and appends the IP address I end up with two sets of keys added to the salt master. I have to go through a renaming of the instance process to clean everything up.

Issue 2) I'm skip Issue 1 because I think there's a user error in there some where. The first thing you will need to do if you would like to try this is to place this script some where on the instance hard drive and make sure it has executable permissions. If you want to get a better understanding on the hostname set script its explained here very well: Long Explanation Short Explanation.

This shit script is used to parse the userdata and IP address then set that as your hostname for your instance. I used location: /usr/share/local/

OUTPUT3="$(/usr/bin/curl -s | awk -F '.' '{ print $3 }')"
OUTPUT4="$(/usr/bin/curl -s | awk -F '.' '{ print $4 }')"
PRINT3="$(for i in $OUTPUT3; do printf "%0*d\n" 3 $i; done)"
PRINT4="$(for i in $OUTPUT4; do printf "%0*d\n" 3 $i; done)"
USER_DATA=`/usr/bin/curl -s`

# Set the host name
hostname "$HOSTNAME""$PRINT3""$PRINT4"
echo $HOSTNAME > /etc/hostname

You will also need to edit: rc.local
So the script will set the hostname at boot:

cat /etc/rc.local

# By default this script does nothing.

exit 0

Once that is done shutdown the instance and create a AMI. Take that NEW AMI ID and add it to your salt-cloud profile.

Also while your in your profile adding a new line to where your userdata file is going to live. There a doc bug in 2015.8.1 it does not list userdata for ES2 provider however its in the source code and it works.

userdata_file: /etc/salt/dev-t2-micro-userdata

cat /etc/salt/dev-t2-micro-userdata

It does not matter what you put in your userdata file but note that this will become the base hostname of your server after the deployment. Example mine will look like: salttest169254

Issue 3) Time to deploy I new instance using salt-cloud.

salt-cloud -p dev-t2-micro salttest01

If all went well you will get a new instance with the hostname that you put in userdata and IP address appended to it asking for key acceptance.

When I list the salt-keys I have two from the same server kinda. One comes from the command line when I provisioned the instance salttest01 and the other is the updated hostname salttest169254 as by default salt-minion generates the key id based off the hostname.

***Do not just update the name tag in AWS***. Use the rename features in salt-cloud to get the AWS instance name and the salt key ID to match up.

This renames the instance name salttest01 to match the salt key id salttest169254:
salt-cloud -a rename salttest01 newname=salttest169254

Delete the new key:
salt-key -d salttest169254

This reboots the instance:
salt-cloud -a reboot salttest169254

Accept the new key:
salt-key -a salttest169254

Everything should match up now.


comments powered by Disqus