Restore Linode machines using terraform and ansible

Version Date Notes By
0.3 2019-07-16 Change document structure. Added LinodeWeMake and LinodeClientes Repository jfm
0.2 2019-07-08 Added requirements section jfm
0.1 2018-12-28 Initial commit jfm

This guide assumes you already have terraform and ansible installed and configured on your machine.

This guide only show you how to restore the server, not how to restore backups. Depending on the server different folders and files must be restored

Requirements

Terraform 0.12.x

Ansible 2.8.x

Preparation

Clone the project for the server you need to restore from gitlab

Server Repository
LinodeWebApps git clone https://gitlab.wemake.pt/it/infrastructure/linode/ansible-terraform-restore.git
LinodeWeMake git clone https://gitlab.wemake.pt/it/infrastructure/linode/ansible-terraform-restore-wemake.git
LinodeClientes git clone https://gitlab.wemake.pt/it/infrastructure/linode/ansible-terraform-restore-clientes.git

Run terraform init inside the project folder. This will install required plugins. (In certain situation (plugin version error) you may need to run terraform init -upgrade)

SSH Keys

Create a keys folder inside the project: mkdir keys

Create a temporary passwordless public/private ssh key inside the keys folder

cd keys

ssh-keygen -o -a 100 -t ed25519 -f keys/terraform -C "devel@wemake.pt"

This key will be used for terraform to ssh into the server

Next copy the IT public key (wemake.pub) to the keys folder. Check google drive for the key

This key will be configured for the devel user.

After the server restore you must delete all keys from the keys folder and never commit the keys

Configurations

Open the file variables.tf and configure accordingly

Variable Value
public_key_path Path for the public key
private_key_path Path for the private key
root_pass root password for the server (check syspass for "Linode default root password")

The rest of the variables depends of the server you are trying to restore. You must check the server documentation

For testing purposes use something like this for the linode_group and linode_label

Variable Value
linode_group web-app-test
linode_label web-app-test

Open the file provider.tf and configure the linode access token - Check bitwarden for "Linode Toke - Terraform"

Open the file ansible/group_vars/all.yml and change the variables as needed

Check & Apply

Check if everything correctly configured by running terraform plan

You should get something like this:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

linode_instance.web-apps: Refreshing state... (ID: 12234139)

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + linode_instance.web-apps
      id:                 <computed>
      alerts.#:           <computed>
      authorized_keys.#:  "1"
      authorized_keys.0:  "ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxx@xxxx"
      backups.#:          <computed>
      backups_enabled:    <computed>
      boot_config_label:  <computed>
      group:              "web-app-test"
      image:              "linode/ubuntu18.04"
      ip_address:         <computed>
      ipv4.#:             <computed>
      ipv6:               <computed>
      label:              "web-app-test"
      private_ip_address: <computed>
      region:             "eu-central"
      root_pass:          <sensitive>
      specs.#:            <computed>
      status:             <computed>
      swap_size:          <computed>
      type:               "g6-nanode-1"
      watchdog_enabled:   "true"

Plan: 1 to add, 0 to change, 0 to destroy.

Always check if the plan will apply any change/destruction Plan: 1 to add, 0 to change, 0 to destroy.

If everything is OK then apply the script by running terraform apply

This can take a few minutes (about 15) so go grab a

At the end be sure to check for errors

Check instalation

Open the Linode Cloud Manager to check the VM

SSH as devel ssh -i <path to IT private key> -p 1022 devel@new_ip_address

Remove the root keys sudo rm /root/.ssh/authorized_keys

Check installed software as needed:

php --version

mysql --version

nodejs --version

npm --version

jspm --version

Check webserver access: http://new_ip_address

Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.