Skip to content

Instantly share code, notes, and snippets.

@davidlares
Last active January 2, 2020 19:41
Show Gist options
  • Save davidlares/a6459a6d678080bb8e5856113633151f to your computer and use it in GitHub Desktop.
Save davidlares/a6459a6d678080bb8e5856113633151f to your computer and use it in GitHub Desktop.
Digital Ocean's basic droplet setup with Ansible
[server]
Here goes your Droplet IP
[server:vars]
ansible_user=root
ansible_ssh_private_key_file=~/.ssh/id_rsa.pub
- hosts: server
remote_user: root
vars:
user: david
packages: ['curl','git','ufw','nano']
key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
tasks:
- name: Create a new user
user:
name: {{ user }}
state: present
groups: sudo
append: true
create_home: true
shell: /bin/bash
- name: Add SSH key
authorized_key:
user: "{{ user }}"
state: present
key: {{ key }}
- name: Update packages
apt: update_cache=yes
- name: Install packages
apt: name={{ packages }} state=latest
- name: UFW SSH
ufw:
rule: allow
name: OpenSSH
- name: UFW Deny
ufw:
state: enabled
policy: deny
@davidlares
Copy link
Author

Before using this, you will have to:

  1. Generate and copy an SSH key to the Droplet Instance.
  2. Edit the /etc/ansible/hosts file (with the "hosts" file content) and your IP address.
  3. Run the playbook like ansible-playbook server.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment