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