forked from stackbill/marketplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle.yaml
261 lines (207 loc) · 8.46 KB
/
moodle.yaml
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
# Install moodle on ubuntu 20.04 and 22.04 LTS
# https://www.linode.com/docs/guides/how-to-install-moodle-on-ubuntu-server-2004/
# https://www.linode.com/docs/guides/how-to-install-moodle-on-ubuntu-22-04/
- name: Moodle Setup
hosts: localhost
tasks:
# 1 - Disable ssh access and restart the ssh service
- name: Restarting sshd
shell: "sed -i 's/#Match User anoncvs/ForceCommand echo Please wait until the installation is completed..../g' /etc/ssh/sshd_config && systemctl restart sshd"
# 2 - Update the system
- name: Updating Packages
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600
# 3 - Check os version
- name: Check os version
shell: lsb_release -rs
register: os_version
# 4 - Install required Packages
- name: Install required Packages
ansible.builtin.apt:
name: software-properties-common
state: present
# 5 - Add the ondrej/php repository
- name: Add the ondrej/php repository
shell: add-apt-repository ppa:ondrej/php
# 6 - Update the system
- name: Updating Packages
ansible.builtin.apt:
update_cache: yes
# 4 - Install required Packages
- name: Install required Packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
update_cache: yes
cache_valid_time: 3600
with_items:
- apache2
- php8.1
- postfix
- php8.1-mysql
- php8.1-fpm
- php8.1-iconv
- php8.1-mbstring
- php8.1-curl
- php8.1-tokenizer
- php8.1-xmlrpc
- php8.1-soap
- php8.1-ctype
- php8.1-zip
- php8.1-gd
- php8.1-simplexml
- php8.1-xml
- php8.1-intl
- certbot
- python3-certbot-apache
# 5 - Start and enable Nginx service
- name: Start and enable Nginx service
ansible.builtin.service:
name: apache2
state: started
enabled: yes
# 6 - Add the mariadb key
- name: Add the mariadb key
shell: apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
# 7 - Add the mariadb repository based on the os release
- name: Add the mariadb repository based on the os release
shell: |
add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mariadb.mirror.liquidtelecom.com/repo/10.6/ubuntu focal main'
when: os_version.stdout == '20.04'
- name: Add the mariadb repository based on the os release
shell: |
add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mariadb.mirror.liquidtelecom.com/repo/10.6/ubuntu Jammy main'
when: os_version.stdout == '22.04'
# 6 - Enable proxy_fcgi and proxy modules
- name: Enable proxy_fcgi and proxy modules
shell: a2enmod proxy_fcgi proxy
# ignore_errors: yes
# 7 - Update the ;max_input_vars = 1000 to 5000 in php.ini file
- name: Update the ;max_input_vars = 1000 to 5000 in php.ini file
shell: sed -i 's/;max_input_vars = 1000/max_input_vars = 5000/g' /etc/php/8.1/apache2/php.ini
# 5 - Install mariadb server
- name: Install mariadb server
ansible.builtin.apt:
name: ['mariadb-server', 'mariadb-client']
state: present
update_cache: yes
# 6 - Start and enable mariadb service
- name: Start and enable mariadb service
ansible.builtin.service:
name: mariadb
state: started
enabled: yes
# 7 - Generate the mysql root password
- name: Generating mariadb password
shell: openssl rand -hex 24
register: rootpassword
# 8 - Store the mysql root password
- name: Storing mariadb password
copy:
dest: "/root/.mariadb_root_password"
content: |
root_mariadb_user = root
root_mariadb_pass = "{{ rootpassword.stdout }}"
# 9 - Secure mariadb installation
- name: Secure mariadb installation
shell: mysql_secure_installation
args:
stdin: |
y
{{ rootpassword }}
{{ rootpassword }}
y
y
y
y
# 7 - Generate the mysql moodle password
- name: Generating mariadb password
shell: openssl rand -hex 24
register: moodlepassword
# 8 - Store the mysql root password
- name: Storing mariadb password
copy:
dest: "/root/.mariadb_moodleadmin_password"
content: |
root_mariadb_user = moodleadmin
root_mariadb_pass = "{{ moodlepassword.stdout }}"
# 10 - Create a database for moodle
- name: Create a database for moodle
shell: mysql -u root -p{{ rootpassword.stdout }} -e "CREATE DATABASE moodle;"
# 11 - Grant privileges to the moodle database
- name: Grant privileges to the moodle database
shell: mysql -u root -p{{ rootpassword.stdout }} -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodleadmin'@'localhost' IDENTIFIED BY '{{ moodlepassword.stdout }}';"
# 12 - Flush privileges
- name: Flush privileges
shell: mysql -u root -p{{ rootpassword.stdout }} -e "FLUSH PRIVILEGES;"
# 13 - Wget the moodle package
- name: Wget the moodle package
shell: wget -c https://download.moodle.org/download.php/direct/stable402/moodle-latest-402.tgz
# 14 - Extract the moodle package
- name: Extract the moodle package
shell: tar -zxvf moodle-latest-402.tgz -C /root/
# 15 - Remove the index.html file from /var/www/html/
- name: Remove the index.html file from /var/www/html/
shell: rm -rf /var/www/html/index.html
# 15 - Copy the moodle directory to /var/www/html/
- name: Copy the moodle directory to /var/www/html/
shell: cp -R /root/moodle/. /var/www/html/
# 16 - Remove the moodle directory from /root/
- name: Remove the moodle directory from /root/
shell: rm -rf /root/moodle
# 15 - Change the permissions of the moodle directory
- name: Change the permissions of the moodle directory
shell: chmod -R 755 /var/www/html
# 16 - Create a moodledata directory
- name: Create a moodledata directory
shell: mkdir /var/moodledata
# 17 - Change the permissions of the moodledata directory
- name: Change the permissions of the moodledata directory
shell: chmod -R 777 /var/moodledata
# 18 - Make a copy of the moodle configuration file
- name: Make a copy of the moodle configuration file
shell: cp /var/www/html/config-dist.php /var/www/html/config.php
# 19 - Edit the moodle configuration file
- name: Edit the moodle configuration file
shell: |
sed -i -e '/^$CFG->dbtype/s/.*/$CFG->dbtype = '\''mariadb'\'';/' \
-e '/^$CFG->dblibrary/s/.*/$CFG->dblibrary = '\''native'\'';/' \
-e '/^$CFG->dbhost/s/.*/$CFG->dbhost = '\''localhost'\'';/' \
-e '/^$CFG->dbname/s/.*/$CFG->dbname = '\''moodle'\'';/' \
-e '/^$CFG->dbuser/s/.*/$CFG->dbuser = '\''moodleadmin'\'';/' \
-e '/^$CFG->dbpass/s/.*/$CFG->dbpass = '\''{{ moodlepassword.stdout }}'\'';/' \
-e '/^$CFG->prefix/s/.*/$CFG->prefix = '\''mdl_'\'';/' \
-e '/^$CFG->dataroot/s/.*/$CFG->dataroot = '\''\/var\/moodledata'\'';/' \
-e '/^$CFG->directorypermissions/s/.*/$CFG->directorypermissions = 02777;/' /var/www/html/config.php
# 20 - Copy the moodle.conf from /usr/local/src/moodle to /etc/apache2/sites-available/
- name: Copy the moodle.conf from /usr/local/src/moodle to /etc/apache2/sites-available
copy:
src: moodle.conf
dest: /etc/apache2/sites-available/
remote_src: yes
# 22 - remove the 000-default-le-ssl.conf and 000-default.conf from /etc/apache2/sites-available/
- name: remove the 000-default-le-ssl.conf and 000-default.conf from /etc/apache2/sites-available/
shell: rm -rf /etc/apache2/sites-available/000-default-le-ssl.conf /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/default-ssl.conf
# 26 - create a cloudstack directory
- name: Creating a directory for shell script
ansible.builtin.file:
path: /opt/moodle
state: directory
# 27 - copy the shell script from /opt/ to /opt/cloudstack
- name: Copy files for shell script
copy:
src: "{{ item.confsrc }}"
dest: "{{ item.confdest }}"
remote_src: yes
with_items:
- { confsrc: '/usr/local/src/moodle/moodle-cleanup.sh', confdest: '/opt/moodle'}
# 28 - Add the shell script to .bashrc
- name: Adding a line for shell script
lineinfile:
path: /root/.bashrc
line: "chmod +x /opt/moodle/moodle-cleanup.sh && /opt/moodle/moodle-cleanup.sh"
state: present
# 29 - Enable the ssh acess and restart the sshd service
- name: Restarting sshd
shell: "sed -i 's/ForceCommand echo Please wait until the installation is completed..../#Match User anoncvs/g' /etc/ssh/sshd_config && systemctl restart sshd"