-
Notifications
You must be signed in to change notification settings - Fork 8
/
bootstrap.yml
95 lines (76 loc) · 2.56 KB
/
bootstrap.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
88
89
90
91
92
93
94
95
---
- hosts: all
become: yes
become_user: root
vars:
SSH_PORT: 22
DEFAULT_PACKAGES:
- ufw
- fail2ban
tasks:
- include_vars: secrets.yml
- debug: msg="Admin username {{ ADMIN_USERNAME }}"
- debug: msg="Ssh port {{ SSH_PORT }}"
- name: change root password
user: name=root password={{ ROOT_PASSWORD | password_hash('sha512') }}
- name: add admin user
user: name={{ ADMIN_USERNAME }} password={{ ADMIN_PASSWORD | password_hash('sha512') }} shell=/bin/bash
- name: add authorized keys for admin user
authorized_key: user={{ ADMIN_USERNAME }} key="{{ lookup('file', item) }}"
with_items: "{{ PUBLIC_KEYS }}"
- name: create sudoers.d directory
file:
path: /etc/sudoers.d
owner: root
group: root
mode: 0755
state: directory
- name: set includedir in sudoers
lineinfile:
dest: /etc/sudoers
line: "#includedir /etc/sudoers.d"
state: present
validate: "/usr/sbin/visudo -cf %s"
- name: create sudoer file for admin
template:
src: sudoers.d.j2
dest: "/etc/sudoers.d/{{ ADMIN_USERNAME }}"
mode: 0440
owner: root
group: root
validate: "/usr/sbin/visudo -cf %s"
- name: update APT package cache
apt: update_cache=yes cache_valid_time=3600
- name: upgrade APT to the latest packages
apt: upgrade=safe
- name: install required packages
apt: state=installed pkg={{ item }}
with_items: DEFAULT_PACKAGES
- name: install extra packages defined in secrets.yml
apt: state=installed pkg={{ item }}
with_items: "{{ EXTRA_PACKAGES|default([]) }}"
- name: setup ufw
ufw: state=enabled policy=deny
- name: allow ssh traffic
ufw: rule=allow port={{ SSH_PORT }} proto=tcp
- name: change ssh port
lineinfile: dest=/etc/ssh/sshd_config
regexp="^Port\s"
line="Port {{ SSH_PORT }}"
state=present
notify: restart ssh
- name: disallow password authentication
lineinfile: dest=/etc/ssh/sshd_config
regexp="^PasswordAuthentication"
line="PasswordAuthentication no"
state=present
notify: restart ssh
- name: disallow root SSH access
lineinfile: dest=/etc/ssh/sshd_config
regexp="^PermitRootLogin"
line="PermitRootLogin no"
state=present
notify: restart ssh
handlers:
- name: restart ssh
service: name=ssh state=restarted