forked from marji/ansible-playbook-linode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision.yml
87 lines (71 loc) · 2.12 KB
/
provision.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
85
86
87
---
# 2015/11/27 by Marji
# 2020/12/05 by Pratik Raj
#
# An ansible playbook to provision a new linode and give it a hostname
# and a FQDN, deploys a public ssh key under its root account.
#
# Usage:
#
# export LINODE_API_KEY=4kfjh59shlhdkjhksdj00sdsTTsskklkjoiRPrbDtgHY
# ansible-playbook --extra-vars server_hostname=hades provision.yml
#
- name: Provision new linode
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vars/config.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_v4:
label: "{{ label }}"
access_token: "{{ token }}"
type: "{{ instance_type }}"
region: "{{ region }}"
image: "{{ instance_image }}"
root_pass: "{{ password }}"
authorized_keys: "{{ root_ssh_pub_key }}"
group: "{{ group }}"
tags: "{{ tag }}"
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[0] }}"
groupname: "linode"
- name: Wait for Linode to listen on port 22
wait_for:
state: started
host: "{{ linode.instance.ipv4[0] }}"
port: 22
- name: Common configuration on the new server
hosts: linode
user: root
vars_files:
- vars/config.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=ssh state=restarted
# vim:ft=ansible: