
Group variables
By default, Ansible will look for group variables in the same directory as the playbook called group_vars for variables that can be applied to the group. By default, it will look for the filename that matches the group name in the inventory file. For example, if we have a group called [nexus-devices] in the inventory file, we can have a file under group_vars named nexus-devices to house all the variables that can be applied to the group.
We can also use a special file named all to include variables applied to all the groups.
We will utilize this feature for our username and password variables. First, we will create the group_vars directory:
$ mkdir group_vars
Then, we can create a YAML file called all to include the username and password:
$ cat group_vars/all
---
username: cisco
password: cisco
We can then use variables for the playbook:
vars:
nexus_devices: {
"nx-osv-1": {
"hostname": "nx-osv-1",
"username": "{{ username }}",
"password": "{{ password }}",
"vlans": [100, 200, 300],
<skip>
"nx-osv-2": {
"hostname": "nx-osv-2",
"username": "{{ username }}",
"password": "{{ password }}",
"vlans": [100, 200, 300],
<skip>