-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook.yml
461 lines (391 loc) · 11.1 KB
/
playbook.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
---
###### Playbook for setting up multi-server LAMP stack #####
# Common tasks for all servers
- hosts: all
become: yes
vars:
db_ip: "192.168.56.10"
auth_ip: "192.168.56.11"
app_ip: "192.168.56.12"
db_user: "demo_user"
db_password: "team3"
db_name: "myapp"
tasks:
- name: Update apt Cache
apt:
update_cache: yes
- name: Install common packages
apt:
name:
- vim
- curl
- git
- nano
state: present
- name: Gather Facts from All hosts
setup:
##### Tasks specific to the database server #####
- hosts: db
become: yes
vars:
db_user: "demo_user"
db_password: "team3"
mysql_root_password: "team3"
db_name: "myapp"
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install MySQL and Python Dependencies
apt:
name:
- mysql-server
- python3-pymysql
- python3-mysqldb
- python3-pip
state: present
- name: Upgrade Pip
pip:
name: pip
state: latest
executable: pip3
- name: Install PyMySQL
pip:
name: PyMySQL
state: present
- name: Start MySQL service
service:
name: mysql
state: started
enabled: yes
- name: Set MySQL Root Password
mysql_user:
name: root
password: "{{ mysql_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
vars:
ansible_python_interpreter: /usr/bin/python3
environment:
PYTHONPATH: /usr/local/lib/python3.8/dist-packages
- name: Create .my.cnf File
copy:
content: |
[client]
user=root
password={{ mysql_root_password }}
dest: /root/.my.cnf
mode: '0600'
no_log: true
- name: Allow remote connections to MySQL
lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
regexp: '^bind-address'
line: 'bind-address = 0.0.0.0'
notify: Restart MySQL
- name: Create MySQL database
mysql_db:
name: "{{ db_name }}"
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Create MySQL user
mysql_user:
name: "{{ db_user }}"
password: "{{ db_password }}"
priv: 'myapp.*:ALL'
host: '%'
state: present
- name: Grant privileges to demo_user
mysql_user:
name: "{{ db_user }}"
password: "{{ db_password }}"
priv: '*.*:ALL'
host: '%'
state: present
- name: Create Scripts Directory
file:
path: /opt/scripts
state: directory
mode: '0755'
- name: Create SQL File for Scripts Table
copy:
content: |
CREATE TABLE IF NOT EXISTS scripts (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
file_path VARCHAR(255) NOT NULL
);
INSERT INTO scripts (name, file_path) VALUES
('Authentication Server Lockout Policy Shell Script', '/opt/scripts/server_lockout.sh');
dest: /tmp/create_scripts_table.sql
- name: Create Scripts Table with File Paths
mysql_db:
name: "{{ db_name }}"
state: import
target: /tmp/create_scripts_table.sql
# vars:
# ansible_become_user: root
- name: Create Server Lockout Script
copy:
src: server_lockout.sh
dest: /opt/scripts/server_lockout.sh
mode: '0755'
- name: Add auth Server's Public Key to authorized_keys
authorized_key:
user: vagrant
state: present
key: "{{ lookup('file', './auth_id_rsa.pub') }}"
handlers:
- name: Restart MySQL
service:
name: mysql
state: restarted
################### auth server ##################
- hosts: auth
become: yes
vars:
db_ip: "192.168.56.10"
db_user: "demo_user"
db_password: "team3"
db_name: "myapp"
# ldap_domain: "example.com"
# ldap_organization: "Example Org"
# ldap_admin_password: "password"
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Required Packages
apt:
name:
- apache2
- php
- php-cgi
- libapache2-mod-php
- php-mbstring
- php-common
- php-pear
- slapd
- ldap-utils
- ldap-account-manager
- mysql-client
- python3-mysqldb
- python3-pip
- net-tools
# - mysql-client
# - python3-mysqldb
# - python3-paramiko
# - python3-pip
# - python3-mysqldb
# - python3-paramiko
state: present
- name: Enable PHP-CGI
command: a2enconf php*-cgi
notify: Restart Apache
- name: Enable Apache service
systemd:
name: apache2
enabled: yes
state: started
- name: Verify OpenLDAP installation
command: slapcat
register: slapcat_result
changed_when: false
# - name: Configure LDAP
# debconf:
# name: slapd
# question: "{{ item.question }}"
# value: "{{ item.value }}"
# vtype: "{{ item.vtype }}"
# loop:
# - { question: 'slapd/password1', value: '{{ ldap_admin_password }}', vtype: 'password' }
# - { question: 'slapd/password2', value: '{{ ldap_admin_password }}', vtype: 'password' }
# - { question: 'slapd/domain', value: '{{ ldap_domain }}', vtype: 'string' }
# - { question: 'shared/organization', value: '{{ ldap_organisation }}', vtype: 'string' }
# notify: Reconfigure LDAP
- name: Install OpenLDAP
apt:
name:
- slapd
- ldap-utils
- libnss-ldap
state: present
- name: Start OpenLDAP service
service:
name: slapd
state: started
enabled: yes
# - name: Configure LDAP
# command: dpkg-reconfigure slapd
# when: false
# - name: Configure OpenLDAP
# ldap_attrs:
# dn: olcDatabase={1}mdb,cn=config
# attributes:
# olcSuffix: dc=example,dc=com
# olcRootDN: cn=admin,dc=example,dc=com
# olcRootPW: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=12') }}"
# notify: Restart LDAP Services
# - name: Set OpenLDAP admin password
# ldap_attrs:
# dn: cn=admin,dc=example,dc=com
# attributes:
# userPassword: "{{ lookup('password', '/dev/null chars=ascii_letters,digits length=12') }}"
# notify: Restart LDAP Services
# - name: Create base organizational units
# ldap_entry:
# dn: "{{ item }}"
# objectClass: organizationalUnit
# loop:
# - ou=users,dc=example,dc=com
# - ou=groups,dc=example,dc=com
# notify: Restart LDAP Services
- name: Install PAM LDAP
apt:
name:
- libpam-ldap
- nslcd
- pamtester
state: present
notify:
- Restart LDAP Services
- name: Configure PAM
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
backup: yes
loop:
- { src: 'pam_common-auth.j2', dest: '/etc/pam.d/common-auth' }
- { src: 'pam_common-account.j2', dest: '/etc/pam.d/common-account' }
- { src: 'pam_common-session.j2', dest: '/etc/pam.d/common-session' }
- { src: 'pam_common-password.j2', dest: '/etc/pam.d/common-password' }
- name: Restart LDAP Services
systemd:
name: "{{ item }}"
state: restarted
loop:
- slapd
- nslcd
ignore_errors: yes
- name: Install MySQL Connector for Python
pip:
name:
- mysql-connector-python
- paramiko
state: present
- name: Generate SSH Key
user:
name: vagrant
generate_ssh_key: yes
ssh_key_bits: 2048
ssh_key_file: .ssh/id_rsa
- name: Fetch Public Key
fetch:
src: /home/vagrant/.ssh/id_rsa.pub
dest: ./auth_id_rsa.pub
flat: yes
- name: Create Script to Fetch and Execute Scripts from DB
copy:
src: fetch_and_execute_scripts.sh
dest: /usr/local/bin/fetch_and_execute_scripts.sh
mode: '0755'
- name: Ensure SSH key exists for vagrant user
user:
name: vagrant
generate_ssh_key: yes
ssh_key_bits: 2048
ssh_key_file: .ssh/id_rsa
handlers:
- name: Restart Apache
systemd:
name: apache2
state: restarted
- name: Reconfigure LDAP
command: dpkg-reconfigure -f noninteractive slapd
- name: Restart LDAP Services
systemd:
name: "{{ item }}"
state: restarted
loop:
- slapd
- nslcd
ignore_errors: yes
############# app server ####################
- hosts: app
become: yes
vars:
db_ip: "192.168.56.10"
auth_ip: "192.168.56.11"
backup_scripts_dir: "/home/vagrant/backup_scripts"
backup_dest_dir: "/home/vagrant/backups"
tasks:
- name: Update apt Cache
apt:
update_cache: yes
- name: Install Apache and PHP
apt:
name:
- apache2
- php
- libapache2-mod-php
- php-mysql
state: present
- name: Start Apache service
service:
name: apache2
state: started
enabled: yes
- name: Copy PHP info file
copy:
content: "<?php phpinfo(); ?>"
dest: /var/www/html/info.php
- name: Create app config file
template:
src: app_config.j2
dest: /home/vagrant/app_config.py
mode: '0644'
#Additional tasks for Management
- name: Install rsync
apt:
name: rsync
state: present
- name: Create backup script directory
file:
path: "{{ backup_scripts_dir }}"
state: directory
mode: '0755'
- name: Create backup script for db-server
copy:
dest: "{{ backup_scripts_dir }}/backup_db.sh"
content: |
#!/bin/bash
rsync -avz --delete vagrant@{{ db_ip }}:/path/to/backup/ {{ backup_dest_dir }}/db/
mode: '0755'
- name: Create backup script for auth-server
copy:
dest: "{{ backup_scripts_dir }}/backup_auth.sh"
content: |
#!/bin/bash
rsync -avz --delete vagrant@{{ auth_ip }}:/path/to/backup/ {{ backup_dest_dir }}/auth/
mode: '0755'
- name: Ensure backup destination directories exist
file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- "{{ backup_dest_dir }}/db"
- "{{ backup_dest_dir }}/auth"
- name: Schedule backup script for db-server
cron:
name: "Backup DB Server"
minute: "0"
hour: "2"
job: "{{ backup_scripts_dir }}/backup_db.sh"
user: vagrant
- name: Schedule backup script for auth-server
cron:
name: "Backup Auth Server"
minute: "0"
hour: "3"
job: "{{ backup_scripts_dir }}/backup_auth.sh"
user: vagrant