Here's an Ansible playbook that I use to spin up and provision DigitalOcean droplets.
There's a longer article to follow, if you're interested – but the salient points are:
- Spin up the droplets with Ansible's DigitalOcean module
- Put their details into Ansible's "in-memory inventory" with Ansible's add_host module
- Use those details when you provision the droplets with the apt module and more.
I used homebrew to install Ansible 2.1 on my OSX.11 MacBook. I needed to revert to dopy 0.3.5 (there's a bug in the 0.3.7 version that comes with Ansible 2.1)
The playbook below
- uses a custom ssh key where necessary
- keeps the ssh keys and the API out of the main file
- takes an external file of names for the hosts
- avoids irritating known-host checking by setting the following variable for each new server ansible_ssh_common_args='-o StrictHostKeyChecking=no'
- sets up apache / php / git on each server, and uses a jinja2 template to make a unique-ish page on each host.
- takes about 90 seconds per server
- goes with a matching "destroyDroplets.yml" playbook
---
- name: provision servers
hosts: local
vars_files:
- ./vars/droplets.yml
- ./vars/sensitive.yml
- ./vars/sshInfo.yml
tasks:
- name: Get DigitalOcean's ID of ssh key
digital_ocean: #note avoidance of = signs...
command: ssh
state: present
name: "{{ sshInfo.do_ssh_key_name}}"
api_token: "{{ sensitive.do_token }}"
register: my_DO_ssh_key
#
- name: make droplets, if they don't exist already
digital_ocean: >
state=present
command=droplet
name={{item.name}}
unique_name=yes
size_id=512mb
region_id=lon1
image_id=ubuntu-14-04-x64
ssh_key_ids={{ my_DO_ssh_key.ssh_key.id }}
api_token={{ sensitive.do_token }}
wait=yes
with_items: "{{droplets}}"
register: droplet_details
#
- name: Add named droplet to group newServers # variables set user (needed), use right key, stop wretched dialog with known_hosts
add_host: >
groupname=newServers
hostname="{{ item.droplet.ip_address }}"
ansible_user=root
ansible_private_key_file="{{sshInfo.local_private_ssh_key}}"
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
WPL_server_info="{{item.droplet.name}}"
otherServers="{{droplet_details.results}}"
with_items: '{{droplet_details.results}}'
#
- name: set up servers
hosts: newServers
tasks:
- name: install packages
apt: >
name={{item}}
state=present
update_cache=yes
with_items:
- apache2
- libapache2-mod-php5
- git
- name: remove existing web stuff
file: >
path=/var/www/html/index.html
state=absent
- name: set up index
template: src=./siteStuff/index.html dest=/var/www/html/index.html force=yes
- name: start Apache
service: name=apache2 state=running enabled=yes
...
If you want to use this, you'll need a DigitalOcean account (get yours here), a DigitalOcean API key, a public/private key pair for ssh (and you'll upload the public one for DigitalOcean to use as you set up, a bunch of configuration files that can be inferred from the playbook, and a template for a web page. Wait about and I'll post them.
No comments:
Post a Comment