-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplaybook.yml
More file actions
314 lines (273 loc) · 7.96 KB
/
Copy pathplaybook.yml
File metadata and controls
314 lines (273 loc) · 7.96 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
---
- name: Setup controller
hosts: localhost
connection: local
tasks:
- name: Set environment variables
command: env
environment: "{{ env_vars }}"
- name: "Ensure ssh key exists"
community.crypto.openssh_keypair:
path: "{{ remote_dev_box_key_path }}"
# AWS only supports RSA keys
type: rsa
mode: 0400
- name: Create terraform vars file
ansible.builtin.template:
src: templates/tfvars.j2
dest: terraform/terraform.tfvars
vars:
inventory_hostname: "{{ hostvars[groups['ec2instances'][0]['inventory_hostname'] }}"
- name: Install pip packages
pip:
name: "{{ packages }}"
vars:
packages:
# botox are needed for AWS
- boto
- boto3
tags:
- local
- name: Upload public key
amazon.aws.ec2_key:
aws_access_key: "{{ env_vars.AWS_ACCESS_KEY_ID }}"
aws_secret_key: "{{ env_vars.AWS_SECRET_ACCESS_KEY }}"
region: "{{ env_vars.AWS_REGION }}"
name: "{{ host_name }}"
key_material: "{{ lookup('file', '{{ remote_dev_box_key_path }}.pub') }}"
force: false
tags:
- local
- name: Terraform apply
community.general.terraform:
project_path: ./terraform
state: present
force_init: yes
register: outputs
- name: Add instance public ip to host group
ansible.builtin.add_host:
name: remote
ansible_host: "{{ outputs.outputs.instance_public_dns.value }}"
ansible_user: "{{ remote_user }}"
remote_dev_box_key_path: "{{ remote_dev_box_key_path }}"
- name: Create local ssh config file
ansible.builtin.template:
src: templates/ssh_config.j2
dest: ssh_config
mode: 0644
- name: Create ssh.sh
ansible.builtin.template:
src: templates/ssh.j2
dest: ssh.sh
mode: 0744
- name: Include local ssh config file
lineinfile:
dest: ~/.ssh/config
line: "Include {{ playbook_dir }}/ssh_config"
create: yes
- name: Add key to ssh-agent
shell: "ssh-add -K {{ remote_dev_box_key_path }}"
- name: Read SSH public key to authorize
ansible.builtin.shell: "cat {{ remote_dev_box_key_path }}.pub"
register: ssh_pub_key
- name: Authorize key with GitHub
local_action:
module: github_key
name: "Access Key for {{ host_name }}"
token: "{{ github_access_token }}"
pubkey: "{{ ssh_pub_key.stdout }}"
- name: Wait for instance to become reachable over SSH
hosts: remote
gather_facts: false
tasks:
- wait_for_connection:
- name: Setup machine
hosts: remote
become: yes
gather_facts: false
tasks:
# https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-18-04
- name: Creating a Swap File
command:
cmd: "fallocate -l {{ swap_in_gb }}G /swapfile"
creates: /swapfile
register: swap_created
- name: Make Swap File only accessible to root
ansible.builtin.file:
path: /swapfile
owner: root
group: root
mode: "0600"
- name: Format file as swap and enable swap
command:
cmd: "{{ item }}"
loop:
- mkswap /swapfile
- swapon /swapfile
when: swap_created.changed
- name: Making the Swap File Permanent
lineinfile:
dest: /etc/fstab
line: /swapfile none swap sw 0 0
create: yes
- name: Tuning Swap Settings
lineinfile:
dest: /etc/sysctl.conf
line: "{{ item }}"
create: yes
loop:
- vm.swappiness=10
- vm.vfs_cache_pressure=50
- name: Start swap as soon as it's created - don't wait for reboot.
command:
cmd: "{{ item }}"
loop:
- sysctl vm.swappiness=10
- sysctl vm.vfs_cache_pressure=50
when: swap_created.changed
- name: Increase inotify watch limit
lineinfile:
dest: /etc/sysctl.conf
line: fs.inotify.max_user_watches=524288
create: yes
- name: Update all packages
yum:
name: "*"
state: latest
update_only: yes
- name: Add extras repository
shell: yum-config-manager --enable extras
- name: Ensure a list of yum packages are installed
yum:
name: "{{ packages }}"
state: latest
update_cache: yes
vars:
packages:
- gcc
- yum-utils
- device-mapper-persistent-data
- lvm2
- amazon-linux-extras
- tmux
- git
- zsh
- htop
# - tig
- name: Install docker-ce (centos) via amazon-linux-extras packages
shell: "amazon-linux-extras install docker=stable -y"
- name: Enable Docker CE service at startup
service:
name: docker
state: started
enabled: yes
- name: Allow remote user to run Docker commands
user:
name: "{{ ansible_user }}"
groups: docker
append: yes
- name: Download docker-compose
ansible.builtin.get_url:
url: https://github.com/docker/compose/releases/download/1.28.6/docker-compose-Linux-x86_64
dest: /usr/local/bin/docker-compose
mode: "0755"
force: yes
- name: set zsh as the default shell
user:
name: "{{ ansible_user }}"
shell: /usr/bin/zsh
- name: Set authorised key
ansible.posix.authorized_key:
user: "{{ ansible_user }}"
state: present
key: "{{ lookup('file', '{{ remote_dev_box_key_path }}.pub') }}"
- name: Setup profile
hosts: remote
tasks:
- name: Copy ssh private key
ansible.builtin.copy:
src: "{{ remote_dev_box_key_path }}"
dest: ~/.ssh/id_rsa
mode: 0600
tags:
- ssh
- name: Install Oh My ZSH
shell: sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
args:
creates: "/home/{{ ansible_user }}/.oh-my-zsh"
- name: Installing Nvm
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.37.2/install.sh | bash
args:
creates: "{{ ansible_env.HOME }}/.nvm/nvm.sh"
tags:
- nodejs
- name: Copy dot files
ansible.builtin.copy:
src: "files/{{ item }}"
dest: "~/.{{ item }}"
loop:
- tmux
- zshrc
tags:
- shell
- name: Copy code-shell.sh
ansible.builtin.copy:
src: code-shell.sh
dest: ~/code-shell.sh
mode: 0744
tags:
- vscode
- name: Installing Node LTS
shell: |
source ~/.nvm/nvm.sh
nvm install v14.15.4
args:
creates: ~/.nvm/versions/node/v14.15.4
tags:
- nodejs
- name: Create VSCode data directory
ansible.builtin.file:
path: ~/.vscode-server/data/Machine
state: directory
tags:
- vscode
- name: Setup VSCode terminal with tmux
ansible.builtin.copy:
content: |
{"terminal.integrated.shell.linux": "/home/{{ ansible_user }}/code-shell.sh"}
dest: ~/.vscode-server/data/Machine/settings.json
tags:
- vscode
- name: Create Code directory
ansible.builtin.file:
path: ~/Code
state: directory
mode: 0755
- name: Git config
git_config:
name: "{{ item.name }}"
scope: global
value: "{{ item.value }}"
loop: "{{ git_config }}"
tags:
- git
- name: Scan and save all SSH host keys
lineinfile:
dest: ~/.ssh/known_hosts
create: yes
line: "{{ lookup('pipe', 'ssh-keyscan {{ item }}') }}"
loop:
- github.com
- gitlab.com
- bitbucket.org
tags:
- git
- name: Just ensuring the repo checkout exists
git:
repo: "{{ item.repo }}"
dest: "~/Code/{{ item.dest }}"
update: no
loop: "{{ repos }}"
tags:
- git