We can further separate out the host variables in the same format as the group variables:
$ mkdir host_vars
In our case, we execute the commands on the localhost, therefore the file under host_vars should be named accordingly, such as host_vars/localhost. Note that we kept the variables declared in group_vars.
---
"nexus_devices":
"nx-osv-1":
"hostname": "nx-osv-1"
"username": "{{ username }}"
"password": "{{ password }}"
<skip>
"netflow_enable": True
"nx-osv-2":
"hostname": "nx-osv-2"
"username": "{{ username }}"
"password": "{{ password }}"
<skip>
After we separate out the variables, the playbook now becomes very lightweight and only consists of the logic of our operation:
$ cat chapter5_9.yml
---
- name: Ansible Group and Host Variables
hosts: localhost
tasks:
- name: create router configuration files
template:
src=./nxos.j2
dest=./{{ item.key }}.conf
with_dict: "{{ nexus_devices }}"
The group_vars and host_vars directories not only decrease our operations overhead. They can help in securing the files, which we will see next.