Introduction to Ansible Transcripts
Chapter: Data
Lecture: Ansible Variables

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're going to modify our existing playbook
0:02 to use variables instead of hardcoding values and tasks.
0:05 This will make our playbooks more maintainable
0:07 and much easier to read.
0:09 Flip back over into your development environment.
0:11 We should be in the same directory
0:13 where we have our first playbook.
0:15 Create a new directory named group_vars.
0:19 Within group_vars, create a file named all.
0:23 In all, we're going to have a bunch of key value pairs
0:25 and a couple of good ones to start with
0:27 are the values for our deployer user
0:29 and the name of the group we have for the deployer user.
0:31 So instead of hardcoding deployer or deployers in our task
0:35 we'll pull that from a variable.
0:37 Your first variable, deploy_user.
0:38 deploy_user will be named deployer.
0:42 A deploy_group as a second key
0:45 the deployers as the value. Save the file
0:50 go back to the route directory of our playbook
0:52 and into roles/common/tasks
0:55 and modify the new_user file.
0:56 Instead of having hardcoded name deployers
1:01 we'll use the variable here.
1:06 And here where we create our non-root user
1:09 we use the deploy_user variable
1:11 and again, deploy_group as our variable.
1:17 Keep going down and then under our user of deploy user
1:22 then we'll save that.
1:23 And there's a couple other places
1:24 where we could use variables.
1:25 For example, which shell we would want
1:27 and this hardcoded path for where our public key is.
1:30 But let's give this a try for now
1:31 just with these two new variables and see how this works.
1:37 Head back into the top level directory
1:40 and let's re-run our playbook.
1:44 Same command as before.
1:48 Okay, and we ran into an issue
1:50 and this will often come up if you have hardcoded values
1:52 and you're placing them with variables.
1:54 The spaces are throwing off Ansible.
1:57 It can't parse what's happening here
1:58 so we need to make sure that we have double quotes
2:00 around our variable names when they're used as values.
2:04 So just head back into roles/common/tasks, modify the user
2:09 and then down here, name, deploy_group
2:12 make sure to put all these in double quotes.
2:18 Cool. Let's re-run this.
2:23 Okay, so I got us past the first error
2:25 and this is a really good time to take a look
2:26 at a common error that will come up when you make a typo.
2:29 So we had a variable defined deploy_user
2:32 but in this case, I typed in deployer_user
2:34 and of course, there is no deployer_user variable.
2:38 So let's fix this one error where we said deployer
2:44 and re-run one more time.
2:49 Awesome. Everything was successful
2:50 and now as you can see, under group_vars
2:52 when we take a look at our variables file
2:54 we now have two variables: deploy_user, deploy_group
2:57 and their values which we can change whenever we want.
3:00 So if I wanted my deploye_user to be named matt
3:02 do that instead and that would then affect every place
3:05 that we've used this variable across our entire playbook.
3:07 Much more maintainable than hardcoding deployer everywhere.