Previously I wrote a post about joining your Ubuntu 16.04 server to Active Directory. This works well and what I have done with one of my clients is create an AMI within AWS that handles most of this. But, it still requires some manual interaction because during the realm join you’ll get prompted for a password.

I’m moving all setup over to a combination of Terraform and Ansible, so this is somewhat a stopper, since it shouldn’t require any user interaction to get things setup. So, I’ve put some code into a script and then use a terraform remote-exec command to handle the actual joining of a domain.

My terraform config for the ec2 setup has this towards the end:

In the above, $hostname is the hostname of the new server, and $ad_password is the active directory password to join the domain for the “linux” user below. I grab the $ad_password out of an environment variable so it’s not stored in code.

The script below is what actually gets executed to join the domain. The key line is the echo ${PASS} | kinit linux. With this, it does not prompt for a password while doing the actual realm join.

A couple things to note above. The default hostname from the AMI, is linux-ami. So, that is changed to the new hostname. The AMI is also joined to the domain, but I want to have the domain join done as the new server. So, we realm leave first before doing a realm join.

We’re also nuking the sssd cache at the end. Without this, the SSSD cache causes some issues, so remote ssh and rdp logins don’t work.