Introduction to Ansible Transcripts
Lecture: Working with Templates
0:00 Head back to the terminal where we're going to modify
0:02 our first playbook to add a template file to it
0:04 and then execute that with Ansible playbook
0:06 to put that template file onto our remote server.
0:09 Go into roles, common, and we currently have just the tasks
0:13 directory, so create a new directory for templates.
0:17 Under templates, create a new file
0:22 sample template.j2
0:23 and we're just going to put some text in here
0:25 with a couple of variables that use the Jinja2 syntax.
0:29 We can have whatever we want in here.
0:30 This could be a configuration file for Nginx
0:33 or another service that we're using
0:34 but in our case we're just going to have a simple text file.
0:36 So, we'll say, this is an example template.
0:46 And we set up a user named deploy_user
0:48 and when Ansible executes instead of having deploy_user
0:51 it's going to replace this token in the template
0:54 with the value of the variable deploy_user
0:57 should be deployer in our case.
1:03 And we'll just have a second variable
1:06 that we've already defined, deploy_group.
1:07 So this is how we can combine boilerplate text
1:10 such as this is an example template
1:12 with the variables that we have defined
1:14 like deploy_user and deploy_group.
1:16 Save this file, and then we're going to go
1:18 into the tasks directory.
1:21 Edit the main.yaml file.
1:25 And we'll include a new file, write_template.
1:29 So save main.yml, and then let's create
1:38 It should have a simple comment
1:39 that this task is going to create
1:41 an example file from our template.
1:45 We'll use a new module template.
1:47 I'm just passing two arguments, a source and a destination.
1:51 The source is going to specify the name of a file
1:54 under our templates directory.
1:55 So Ansible knows to go to look in the templates directory
1:58 of the same role that this task is in.
2:04 The source is the template that we just wrote.
2:07 And we want to write the destination location
2:10 on a remote server we're running our playbook against.
2:12 And this can also use variables, so we can have this
2:15 right under the deploy_user variable
2:18 so this will be home/deployer
2:19 and we can choose to give it a different file name
2:22 than our template, because most likely
2:23 we're not going to want to use the j2 extension.
2:28 Let's call it our example output. Okay.
2:31 Now we should be able to run this playbook again.
2:42 All right, we see that the new task right_example
2:44 template file was executed, and executed successfully
2:48 and it changed something on a remote server.
2:50 So let's take a look and see if that file
2:51 has now been written.
2:53 We can SSH into our remote server.
3:03 And we're ready in our home directory, home/deployer.
3:06 We can see our example output.
3:09 This is an example template.
3:10 We set up a user named deployer, so the token
3:13 for deploy_user was replaced with deployer
3:16 and a group named deployers, and deployers replaced that
3:19 deploy_group token.
3:21 So that's how we can use templates to populate files
3:24 such as configuration files or read.me files
3:26 whatever we need, on our remote servers.
3:28 And we can use our variables as input
3:30 into those template files.
3:32 This is a very common pattern.
3:33 We'll use templates extensively in the remainder
3:36 of the videos for this course.