Last Updated: February 25, 2016
· nosolopau

Setting Ansible playbook variables from Makefile

Digital Ocean's droplets are configured by default with root user, and the SSH keys are set for this account. However, we usually want to disable the remote root access for our server and use a different administrator user instead, so basically we have two options: (A) run a bootstrap script in the remote machine to create an administrator user or (B) pass an argument to the server make command to switch between users. The last option will allow us to use a cleaner and simple install process, as described bellow.

For the first install (this will run under root user, installing the base system and then disabling root access):

make myserver install

For the subsequent installations (with the administrator user) we'll use:

make myserver

We can get the command line arguments within the Makefile with something like:

  ifneq (,$(findstring install,$(MAKECMDGOALS)))
    ansible-playbook -l myserver playbook-myserver.yml -vvvv -e install=true
    ansible-playbook -l myserver playbook-myserver.yml -vvvv

And in the Ansible playbook we can select the user based on the install param:

- hosts: myserver
  sudo: true
  remote_user: "{{'root' if install is defined else 'administrator'}}"

That's all :)