-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast-setup.yml
163 lines (112 loc) · 5.65 KB
/
last-setup.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
---
- name: Setup psql and restarting apache (Created by https://github.com/Itsmide)
hosts: database
become: yes
remote_user: root
vars_files:
- ubuntu20
tasks:
- name: Change Directory to /etc/apache2/sites-available
shell: cd /etc/apache2/sites-available
- name: Removing Default
shell: sudo a2dissite 000-default.conf
- name: Assigning New File (Created by https://github.com/Itsmide)
shell: sudo a2ensite laravel.conf
- name: Rewriting File
shell: sudo a2enmod rewrite
- name: Reloading apache
shell: sudo systemctl reload apache2
- name: Creating File (Created by https://github.com/Itsmide)
ansible.builtin.copy:
dest: /root/hello.sql
content: |
create role psqluser with login;
create database hello_postgres with owner psqluser;
\c hello_postgres psqluser;
create table hello( tag_name varchar(32), tag_value text
);
insert into hello (tag_name, tag_value) values('Hello', 'Postgresql');
- name: Creating bash script file for psql (Created by https://github.com/Itsmide)
ansible.builtin.copy:
dest: /root/psql-install.sh
content: |
#!/usr/bin/bash
# Section 1 - Variable Creation
echo "Creating variables for use throughout the PSQL installation process"
# $packages is an array containing the dependencies for PostgreSQL
packages=('git' 'gcc' 'tar' 'gzip' 'libreadline5' 'make' 'zlib1g' 'zlib1g-dev' 'flex' 'bison' 'perl' 'python3' 'tcl' 'gettext' 'odbc-postgresql' 'libreadline6-dev')
# $rfolder is the install directory for PostgreSQL
rfolder='/postgres'
# $dfolder is the root directory for various types of read-only data files
dfolder='/postgres/data'
# $gitloc is the location of the PosgreSQL git repo
gitloc='git://git.postgresql.org/git/postgresql.git'
# $sysuser is the system user for running PostgreSQL
sysuser='postgres'
# $helloscript is the sql script for creating the PSQL user and creating a database.
helloscript='/home/leewalker/scripts/hello.sql'
# $logfile is the log file for this installation.
logfile='psqlinstall-log'
# Section 2 - Package Installation
# Ensures the server is up to date before proceeding.
echo "Updating server..."
sudo apt-get update -y >> $logfile
# This for-loop will pull all packages from the package array and install them using apt-get
echo "Installing PostgreSQL dependencies"
sudo apt-get install ${packages[@]} -y >> $logfile
# Section 3 - Create required directories
echo "Creating folders $dfolder..."
sudo mkdir -p $dfolder >> $logfile
# Section 4 - Create system user
echo "Creating system user '$sysuser'"
sudo adduser --system $sysuser >> $logfile
# Section 5 - Pull down PSQL using git
echo "Pulling down PostgreSQL from $gitloc"
git clone $gitloc >> $logfile
# Section 6 - Install and configure PSQL
# Configuring PostgreSQL to be installed at /postgres with a data root directory of /postgres/data
echo "Configuring PostgreSQL"
~/postgresql/configure --prefix=$rfolder --datarootdir=$dfolder >> $logfile
echo "Making PostgreSQL"
make >> $logfile
echo "installing PostgreSQL"
sudo make install >> $logfile
echo "Giving system user '$sysuser' control over the $dfolder folder"
sudo chown postgres $dfolder >> $logfile
# InitDB is used to create the location of the database cluster, for the purpose of this exercise it will be placed in the $dfolder under /db.
echo "Running initdb"
sudo -u postgres $rfolder/bin/initdb -D $dfolder/db >> $logfile
# Section 7 - Start PSQL
# PostgreSQL is being started, using pg_ctl as the system user postgres.
echo "Starting PostgreSQL"
sudo -u postgres $rfolder/bin/pg_ctl -D $dfolder/db -l $dfolder/logfilePSQL start >> $logfile
# Section 8 - Add PostgreSQL to /etc/rc.local and add environment variables to /etc/profile
# The command to start PostgreSQL at launch is added to /etc/rc.local, again using the system user postgres.
echo "Set PostgreSQL to launch on startup"
sudo sed -i '$isudo -u postgres /postgres/bin/pg_ctl -D /postgres/data/db -l /postgres/data/logfilePSQL start' /etc/rc.local >> $logfile
# This block adds the environment variables for PostgreSQL to /etc/profile in order to set them for all users.
echo "Writing PostgreSQL environment variables to /etc/profile"
cat << EOL | sudo tee -a /etc/profile
# PostgreSQL Environment Variables
LD_LIBRARY_PATH=/postgres/lib
export LD_LIBRARY_PATH
PATH=/postgres/bin:$PATH
export PATH
EOL
# Section 8 - hello.sql script is ran
echo "Wait for PostgreSQL to finish starting up..."
sleep 5
# The hello.sql script is ran to create the user, database, and populate the database.
echo "Running script"
$rfolder/bin/psql -U postgres -f $helloscript
# Section 9 - hello_postgres is queried
echo "Querying the newly created table in the newly created database."
/postgres/bin/psql -c 'select * from hello;' -U psqluser hello_postgres;
- name: Changing File Permission
shell: chmod u+x /root/psql-install.sh
- name: Change Directory to Root (Created by https://github.com/Itsmide)
shell: cd /root
- name: Execute Script and Install Postgresql
shell: ./psql-install.sh
- name: Finished (Created by https://github.com/Itsmide)
shell: cd ~