Deploy VULTR Servers with Ansible

From Knowledge Center
Revision as of 12:04, 1 May 2020 by Ruan (talk | contribs) (Created page with "This will demonstrate how to deploy a Server to VULTR and install packages on Ubuntu 18 using Ansible. ==Setup== Set the following environment variables: <pre> $ export VUL...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This will demonstrate how to deploy a Server to VULTR and install packages on Ubuntu 18 using Ansible.

Setup

Set the following environment variables:

$ export VULTR_API_KEY=<redacted>
$ export VULTR_API_TIMEOUT=30
$ export VULTR_API_CONFIG=~/.vultr.ini

The ansible.cfg file:

[defaults]
host_key_checking=False
[inventory]
enable_plugins=vultr

The vultr.yml file:

---
plugin: vultr

Your localhost host variables: host_vars/localhost.yaml:

---
vultr_common:
  apikey: "<redacted>"
  min_balance: -5
  os: "Ubuntu 18.04 x64"
  plan: "1024 MB RAM,25 GB SSD,1.00 TB BW"
  region: "Amsterdam"
  sshkey_name: "ruan-laptop"

Our SSH key are in place for our home user (and the public key is added to VULTR):

$ ls ~/.ssh/id_rsa
/root/.ssh/id_rsa

Our deployment playbook deploy.yml:

- hosts: localhost
  #gather_facts: false
  tasks:
  - name: "Set vars"
    set_fact:
      selected_apikey: "{{ vultr_common.apikey }}"
      selected_servername: "{{ vultr_server_name }}"
      selected_hostname: "{{ vultr_server_name }}"
      selected_sshkey_name: "{{ vultr_common.sshkey_name }}"
      selected_os: "{{ vultr_common.os }}"
      selected_plan: "{{ vultr_common.plan }}"
      selected_region: "{{ vultr_common.region }}"
      #target_group: "group"

  - name: "Get and register account info"
    vultr_account_info:
    register: result

  - name: "Print account info"
    debug:
      var: result.vultr_account_info

  - name: "Account balance requirement check"
    fail:
      msg: "Account balance low: {{ result.vultr_account_info.balance|int }}"
    when: (result.vultr_account_info.balance|int > vultr_common.min_balance)

  - name: "Account balance is good"
    debug:
      msg: "Balance Value: {{ result.vultr_account_info.balance|int }}"

  - name: "Ensure an SSH key is present"
    delegate_to: localhost
    vultr_ssh_key:
      api_key: "{{ selected_apikey }}"
      name: "{{ selected_sshkey_name }}"
      ssh_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

  - name: "Create Server if not exiting"
    delegate_to: localhost
    vultr_server:
      name: "{{ vultr_server_name }}"
      os: "{{ selected_os }}"
      plan: "{{ selected_plan }}"
      ssh_keys:
        - "{{ selected_sshkey_name }}"
      region: "{{ selected_region }}"
      state: present

  - name: "ensure a server is present and started"
    local_action:
      module: vultr_server
      name: "{{ vultr_server_name }}"
      os: "{{ selected_os }}"
      plan: "{{ selected_plan }}"
      region: "{{ selected_region }}"
      state: started

  - name: "VULTR account facts"
    local_action:
      module: vultr_account_info

  - name: "Get public IP of ansible host"
    local_action:
      module: ipify_facts
    when: not(ansible_facts.ipify_public_ip is defined)

- hosts: "{{ vultr_server_name }}"
  tasks:
  - name: create the 'ubuntu' user
    user: name=ubuntu append=yes state=present createhome=yes shell=/bin/bash

  - name: set up authorized keys for the ubuntu user
    authorized_key: user=ubuntu key="{{item}}"
    with_file:
      - ~/.ssh/id_rsa.pub

  - name: update cache
    apt:
      update_cache: yes

  - name: install packages
    apt:
      pkg:
        - ntp
        - build-essential
        - vim
        - git
      state: present
      autoclean: yes

  - debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']

Our destroy playbook destroy.yml:

- hosts: localhost
  tasks:
  - name: "Set vars"
    set_fact:
      selected_apikey: "{{ vultr_common.apikey }}"
      selected_servername: "{{ vultr_server_name }}"
      selected_hostname: "{{ vultr_server_name }}"
      selected_os: "{{ vultr_common.os }}"
      selected_plan: "{{ vultr_common.plan }}"
      selected_region: "{{ vultr_common.region }}"
      selected_sshkey_name: "{{ vultr_common.sshkey_name }}"

  - name: delete server
    delegate_to: localhost
    vultr_server:
      name: "{{ vultr_server_name }}"
      os: "{{ selected_os }}"
      plan: "{{ selected_plan }}"
      ssh_keys:
        - "{{ selected_sshkey_name }}"
      region: "{{ selected_region }}"
      state: absent

Demo

Let's deploy our Ubuntu 18.04 server called my-server on VULTR and let ansible update and install our defined packages:

$ ansible-playbook -i vultr.yml -e vultr_server_name=my-new-server deploy.yml

And to destroy the server:

$ ansible-playbook -i vultr.yml -e vultr_server_name=my-new-server destroy.yml

Resources