Introduction to Ansible Transcripts
Chapter: Data
Lecture: Environment Variables

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we know how to set variables
0:02 in separate files from our roles
0:04 we also need to know how to read environment variables.
0:07 Those are variables set on the system
0:09 that wouldn't be hard-coded in any files.
0:11 What do we mean by an environment variable?
0:13 Let's say you're in your shell
0:14 and you want to set a variable
0:15 that you can read across the system.
0:17 You'd use the export command and then a variable name
0:21 and then followed by a value.
0:25 Now we can read to this environment variable
0:29 and using the echo command
0:31 print it out to the command line.
0:32 So we set the value of ansible.out to the key filename.
0:36 Let's set another environment variable.
0:38 This time it'll be more useful.
0:39 In our first playbook we upload a public SSH key
0:42 as our authorized key
0:44 and that allows us to log in with our private key
0:46 but we hard-coded where that key was stored
0:48 which really should be set as a variable
0:50 Whether stored in a file or as an environment variable.
0:52 Let's set it as an environment variable.
0:54 We'll read it into our Ansible playbook
0:55 and use it as a part of our task.
0:58 So we'll say authorized key
1:08 and we'll give it the same authorized key
1:09 that we used in the last chapter.
1:12 So now that authorized key is set
1:15 we can read it into our Ansible playbook.
1:18 Open up group_vars/all
1:23 and set a new authorized key variable name.
1:25 Now we're going to use the lookup template tag
1:30 to look up environment variable named authorized key.
1:37 And let's actually call this authorized key filename
1:41 to differentiate it from the contents
1:43 of an authorized key file.
1:46 So now that we have authorized key filename
1:48 we can go back into our roles common tasks
1:52 modify new user.
1:55 Go down to the key and we'll change lookup file
1:58 instead of setting this through a string
2:00 we're going to have our variable name here.
2:07 Save that, go back up to the top project directory
2:11 now we're in our playbook.
2:16 Awesome, so Ansible was able to get
2:18 the environment variable that we had set as authorized key
2:20 read it in as the variable authorized key filename
2:23 and we were able to use it in one of our tasks
2:26 as part of our playbook.