Introduction to Ansible Transcripts
Chapter: Deployments
Lecture: Updating Config Files with the lineinfile Module
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
We set up the WSGI server
0:01
and now have Nginx serving as a reverse proxy.
0:03
We grabbed our application dependencies from PyPI.
0:06
One more step that we want to do to make sure
0:08
that we can actually have a two-server deployment
0:10
is tweak our database settings.
0:12
Right now, Postgres, very same default settings
0:15
only allow local connections.
0:17
And so, we just want to update the configuration
0:19
so that our web server can connect to the database server.
0:22
We're just going to tweak one line
0:24
in our Postgres configuration
0:25
and that will do it for our deployment.
0:28
So our web server is set
0:29
so let's move into roles/database/tasks.
0:36
And we'll add one task here.
0:43
Allow connections from our web server.
0:45
Could of used the lineinfile module
0:47
and the lineinfile module checks to see
0:49
if a specific line is in a file
0:51
and if it is skips the step
0:53
if it's not I'll make sure that line gets in there.
0:55
It's super handy for tweaking configuration files
0:57
and especially if you don't want to create
0:59
an entire template file yourself.
1:02
First, we specify the path to the file that we want to modify.
1:11
And modify the pg_hba.conf Postgres figuration file.
1:17
We want this line to be present where we say
1:19
insert this after this line.
1:23
And our line should be
1:36
and normally you'd set up a variable for this
1:38
but in this case we're just going to put
1:39
the IP address of our web server
1:42
and we're going to trust it.
1:43
Course, you should modify this for your own purposes
1:46
but this is just an example so that you will be able
1:48
to use the lineinfile module as a task.
1:50
Finally, we need superuser privileges
1:52
so set become yes on this.
1:56
Now, I've tested this out and I have two separate windows.
1:59
In the bottom window, we're going to log into the server.
2:10
Let's take a look at that configuration file.
2:22
So when we execute our playbook
2:23
we are then going to have a line that is inserted here
2:26
into the default configuration that allows our web server
2:29
to connect to the database. We'll just start at the task.
2:36
Now we see that using the lineinfile module
2:39
we can populate text into any files that we want
2:41
whether that's a configuration file, readme file
2:44
we don't always need to use templates to accomplish
2:46
populating information into files.
2:48
Again, for your own deployments
2:50
you're going to want to customize everything
2:51
that we've done here
2:52
including adding additional security settings for Postgres.
2:55
These examples were really to show you what you could do
2:57
with Ansible rather than teach you how to configure
3:00
a Postgres database or set up a Python application.
3:02
This was really just an example to learn more about Ansible.