-
Notifications
You must be signed in to change notification settings - Fork 8
/
provision-server.yml
84 lines (70 loc) · 2.31 KB
/
provision-server.yml
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Usage:
# ansible-playbook --extra-vars "linode_plan=1 server_hostname=thunder3" provision-server.yml
# linode plans, 1 = 1 core $5/mo, 4 = 4 cores $40/mo, etc.
- name: Provision new linode
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vars/conf.yml
tasks:
- name: Fail if server_hostname is not defined
fail: msg="server_hostname needs to be defined via commandline, e.g. --extra-vars server_hostname=flower"
when: server_hostname is not defined
- name: Create linode server via Linode API
# http://docs.ansible.com/ansible/linode_module.html
linode:
name: "{{ server_hostname }}"
# Not strictly necessary, but best to be explicit about where the api key is coming from.
api_key: "{{ lookup('env', 'LINODE_API_KEY') }}"
plan: "{{ linode_plan }}"
datacenter: "{{ linode_datacenter }}"
distribution: "{{ linode_distribution }}"
ssh_pub_key: "{{ root_ssh_pub_key }}"
swap: "{{ linode_swap }}"
wait: yes
wait_timeout: 600
state: present
register: linode
- name: Show me the registered linode
debug: var=linode
- name: Add new host to in-memory inventory
add_host:
hostname: "{{ linode.instance.ipv4 }}"
groupname: "linode"
- name: Wait for Linode to listen on port 22
wait_for:
state: started
host: "{{ linode.instance.ipv4 }}"
port: 22
- cloudflare_dns:
zone: "{{ server_domain }}"
record: "{{ server_hostname }}"
type: A
value: "{{ linode.instance.ipv4 }}"
account_email: "{{ lookup('env', 'CF_EMAIL') }}"
account_api_token: "{{ lookup('env', 'CF_KEY') }}"
register: record
- name: Common configuration on the new server
hosts: linode
user: root
vars_files:
- vars/conf.yml
vars:
server_fqdn: "{{ server_hostname }}.{{ server_domain }}"
tasks:
- name: set hostname
hostname: name="{{ server_hostname }}"
- name: set FQDN
lineinfile:
dest=/etc/hosts
regexp='{{ item }}$'
line="{{ item }} {{ server_fqdn }} {{ server_hostname }}"
state=present
with_items: groups['linode']
- name: ssh key only
copy: src=files/ssh/sshd_config dest=/etc/ssh/sshd_config
notify: restart ssh
handlers:
- name: restart ssh
service: name=sshd state=restarted