From the course: Ansible for Automation Essential Training: Advanced Playbooks, Roles, and Diverse Hosts

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Using variables

Using variables

- [Instructor] Variables are used in all but the most simple playbooks that I create. They add reusability and flexibility, which can make automation much more efficient. I'm going to use the same simple-playbook from the last section and build on it. I'll create a new user, but I'll utilize variables. I'll duplicate my playbook and name it variables.yml. I'll modify the name portion down here in the task to use a variable named new_user. Double mustaches, double quotes, new_user. I could use the playbook as is if I were to run the playbook and present the new_user variable as an extra var like so, ansible-playbook -i, specifying the inventory again. Now, it's going to be -e for the new vars, new_user=test1. And then the playbook, variables.yml. So as demonstrated, even though I didn't define the contents of new variable in the playbook, it was passed as an extra var and ran successfully. There are currently 22 different levels of variable precedence in Ansible, which sounds…

Contents