Script your server

Ken Andries • December 1, 2022

Setting up a server or even just a new application is not something you do on a daily basis (or you may). We all done this before setup a server go through a bunch of commands, scripts and tutorials. Especially as a developer managing a small server.

Every few months or years you need to setup a new server, rebuild your server and then have to figure out what you had installed. How to configure things. Hopefully you got a list, if you're lucky you kept it up-to-date. Most likely not.

Instead of going through the table again and again, automate the process. Even if this is a one-time thing. Using the infrastructure as code principle and using tools like Ansible will make your life lot easier. When six months down the line you need to know how you configured your Nginx or PHP, then you can quickly check your ansible scripts.

The only gotcha to this method is limiting what you do manually on the machine. In ideal scenario you never SSH into the system, you use ansible playbooks for everything.

How to get started?

A question often raised with this process is how do you get started? Start small if you need to setup a new server, use excellent ansible roles created by the community to perform the critical 5 minutes on a new server.

A playbook I have personally used is the following


- name: Common stack hosts: all become: yes vars_files: - vars/security.yml - vars/tools.yml roles: - geerlingguy.swap - - geerlingguy.firewall - geerlingguy.ntp - common-tools


 - "22"
 - "443"
# - "80" # If your still using port 80

You can use ansible-galaxy install {role} to install the roles in your project.

The common-tools role is very simple ansible role to install utilities you often need on server like vim, htop, etc... For those wondering how this role works, it's a single step which ensures the tools you need are present on the box. If they are missing, they will be installed.


- name: Ensure common CLI tools are installed
    state: present
    name: '{{ apt_common_tools }}'


 - vim
 - wget
 - htop
 - atop
 - iotop
 - nethogs
 - curl
 - git
 - unzip

Most of the tools should be part of your default server installation, just in case we make sure they are all present.

The last step is to git init and commit your ansible setup. I know there is a lot more to it than this. Just try to take it one step at a time.

What's next?

If you want to learn more about ansible I can recommend Jeff Geerling ansible resources. Or if you are a visual learner like me, A playlist is made on YouTube.