cri.dev
about posts rss

Minimal Ansible Playbook to provision a server

Published on

In this blog post, we’ll create a minimal Ansible playbook that will allow you to quickly and easily provision a new server.

With just a few simple steps, you’ll be able to set up a server with all the necessary software and configurations in no time.

Install Ansible

You can do this by following the instructions on the Ansible installation page.

But generally a quick python3 -m pip install --user ansible should do the trick.

Create an inventory file

This is a simple text file that contains the IP addresses of the servers that you want to provision, plus some connection settings.

To create a new inventory file, create a new file called production and add the IP address of your server to it:

server1 ansible_host=<ENTER_YOUR_SERVER_IP_ADDRESS> ansible_user=root ansible_connection=ssh ansible_ssh_private_key_file=<PATH_TO_YOUR_SSH_PRIVATE_KEY> ansible_ssh_port=22

In this file we defined a server creatively named server1 and set some connection settings for it, like the user, ssh private key path etc.

Create a new Ansible playbook

Next, we’ll create a new Ansible playbook. This is a simple YAML file that contains all the instructions for provisioning the server.

To create a new playbook, create a new file called playbook.yml and add the following content:

---
- hosts: all
  become: yes
  tasks:
    - name: install nginx
      apt:
        name: nginx
        state: present

Run the playbook

Now that we have our playbook, we can run it to provision our server.

To run the playbook, run the following command:

ansible-playbook -i production playbook.yml

And that’s it! You now know how to create a minimal Ansible playbook to provision a server.

Here, have a slice of pizza 🍕