Tutorial – How to build your own server infrastructure using Ansible

Introduction

In this article you will learn how to install your own server infrastructure (in this tutorial it’s Docker) on a remote server (in this tutorial we will do it in the Google Cloud) using Ansible without executing a single command on the remote server manually.

Ansible is an open source software and infrastructure automation programme which uses SSH among others to distribute it’s commands. If you want to find out more about how Ansible works click here.

Prerequisits

Install Ansible

First of all you need to install Ansible on your local computer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Install ansible

sudo apt-get update

sudo apt-get install software-properties-common

sudo apt-add-repository --yes --update ppa:ansible/ansible

sudo apt-get install ansible

# Test if it's working

ansible --version

# First Command

ansible all -i localhost, --connection=local -m ping

The last command basically tells Ansible to target all systems for the inventory host localhost and use your local console (not ssh) to run the module ping.

The response from Ansible should be

1
2
3
4
5
6
7
localhost | SUCCESS => {

"changed": false,

"ping": "pong"

}

Editing your hosts which Ansible targets

Next you edit your hosts file in /etc/ansible/hosts. This is the default file Ansible uses to look for any declared hosts (and groups) where it should execute the given commands remotely.

1
sudo nano /etc/ansible/hosts

Append the following lines to the file and save changes

1
2
[local]
localhost

The brackets specify the host group in which your computer is in. You can replace “all” in the following command with “local”

1
2
3
#Execute Command with your adjusted inventory file

ansible all --connection=local -m ping

The response should be

1
2
3
4
5
6
7
localhost | SUCCESS => {

"changed": false,

"ping": "pong"

}

Setting up a remote server

For our remote server we use the Google Cloud Plattform to deploy our Ansible test programme there. Therefore create a Google Compute Engine instance with an Ubuntu distribution and don’t forget to add your SSH key to the instance. Here’s an article on how you create a virtual machine in the Google Cloud and access it via SSH.

After you created the compute instance you can test if it worked by trying to connect from your terminal to it using ssh:

1
ssh -i ~/.ssh/YOUR_SSH_PRIVATE_KEY_FILE YOUR_USERNAME@IP_ADDRESS_OF_VIRTUAL_MACHINE

You should be able to connect to your virtual machine by now.

Configuring Ansible for the remote server

Next edit your hosts file in /etc/ansible/hosts.

1
sudo nano /etc/ansible/hosts

Append the following lines to the file and save changes

1
2
3
4
5
6
7
[remote]
remote_test

[remote:vars]
ansible_host=IP_ADDRESS_OF_VIRTUAL_MACHINE
ansible_ssh_private_key_file=~/.ssh/YOUR_SSH_PRIVATE_KEY_FILE
ansible_user=YOUR_USERNAME

To test if Ansible can connect to your remote compute instance via SSH type the following command

1
ansible remote -m ping

The result should be

1
2
3
4
5
6
7
remote_test | SUCCESS => {

"changed": false,

"ping": "pong"

}

Next we create a playbook for Ansible which is the normal way how to tell Ansible which commands to execute in which order on the remote server. The playbook is in a .yml file format and has a specific format to follow. You can find more out about playbooks in the official Ansible documentation.

1
nano my-playbook.yml

Insert the following code in it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---
- name: install docker
  hosts: remote
  become_method: sudo
  become_user: root
  vars: #local variables
    docker_packages:
      - apt-transport-https
      - ca-certificates
      - curl
      - software-properties-common
   
  tasks:
  - name: Update apt packages
    become: true #make sure you execute the task with sudo privileges
    apt: #use apt module
      update_cache: yes #equivalent of apt-get update

  - name: Install packages needed for Docker
    become: true
    apt:
      name: "{{ docker_packages }}" #uses our declared variable docker_packages
      state: present #indicates the desired package state
      force_apt_get: yes #forces to use apt-get

  - name: Add official GPG key of Docker
    shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  - name: Save the current Ubuntu release version into a variable
    shell: lsb_release -cs
    register: ubuntu_version #Output gets stored in this variable

  - name: Set right Docker directory
    become: true
    shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ubuntu_version.stdout }} stable"

  - name: Update apt packages
    become: true
    apt:
      update_cache: yes

  - name: Install Docker
    become: true
    apt:
      name: docker-ce
      state: present
      force_apt_get: yes

  - name: Test Docker with hello world example
    become: true
    shell: docker run hello-world
    register: hello_world_output

  - name: Show output of hello word example
    debug: #use debug module
      msg: "Container Output: {{hello_world_output.stdout}}"

This playbook basically tells Ansible to install Docker in several steps.

Then execute it with

1
ansible-playbook my-playbook.yml -l remote

Afterwards you should see some magic happen (can take a while) and at the end of the output in your terminal you should find “Hello from Docker!” somewhere in the last debug message.

Congrats! You made it.

Final thoughts

As you see Ansible is a cool software to install other software remotely and scale it in the Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

35 + = 38

Menu