Introduction to Ansible Transcripts
Chapter: Configuring Servers
Lecture: PostgreSQL Management with the postgresql_db and postgresql_user Modules
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We have Postgres installed. Now we want to create a new user for Postgres and create a specific database that we can connect to.
0:09
This is going involve two steps. First, we are going to create some new variables. So go under group_vars. We can just add these to all.
0:16
We're going to have three new variables. First will be the name of the database that we want to create. Let's call it chapter_six.
0:24
We could always go back and change it later create a new database. Typically your database name is going to be the same name as your application
0:30
or something that's easily identifiable. chapter6 is fine in this case. Open database user and we are going introduce something here.
0:37
We can actually use variables within the variables files. So we can have our database user be the same as our deploy user.
0:47
That will just populate deployer into the database user. We'll have a database password. And normally, you'd encrypt this.
0:59
We'll just use this in plain text as we are playing around with our files and just getting everything up and running.
1:04
All right, with those three new variables in place let's save the file. Head back into roles, database, tasks.
1:14
And into Postgres. We'll have two new tasks here. First, we are going to create a new database instance.
1:24
And, like a package manager, this will only create a new database instance if one has not already been created. Use the postgresql_db module
1:39
populate in our variable. We are going to want to use the Postgres user in order to create our database. So say become: yes. and become_user: postgres.
1:48
All right, now we will want to create a separate Postgres user. In the postgresql_user module.
2:05
Now we already have our database configured. db is for the database name name is for the user that we want to create, dbuser.
2:13
And remember if you want to take a look at all the options that are available take a look at the documentations modules page
2:18
for postgresql_db and postgresql_user. And just give all privileges for simplicity. In your case, you're going to want to make sure
2:35
that you're strict privileges based on whatever user that you're creating. All right and one typo to fix become_user is the appropriate way
2:55
to specify different user name. Let's give this a try.
3:15
All right, so this may be a confusing failure message because it says the Python psycopg2 module is required. But didn't we already install this?
3:23
The answer is yes. But, this is actually another one of those Python 2 Python 3 issues that can come up when using Ansible.
3:30
So we installed the Python 2 version of psycopg2. We really want the Python 3 version. That way the Python 3 installation on remote machines
3:38
that we're using has access to psycopg2. Let's go back under roles/database/tasks and we want the Python 3 version. Let's try this one more time.
4:11
All right, and it looks like that solved the problem. That's one thing that I really wanted to identify because it will bite you if you're not aware
4:18
of the fact that some modules which rely on Python packages want to make sure that you install the correct one, whether that's for Python 3
4:27
or if you're using an operating system that's still relying on Python 2 you install those packages. At least now that you are aware of it
4:33
you should be able to identify these issues if they come up for you much faster than you would if you didn't know about it.
4:38
Next, we'll go ahead and test that our database user has been created then connect the database properly.