Introduction to Ansible Transcripts
Chapter: Data
Lecture: Working with Templates

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

Talk Python's Mastodon Michael Kennedy's Mastodon