Host variables

We can further separate out the host variables in the same format as the group variables. This was how we were able to apply the variables in the Ansible 2.5 playbook examples in Chapter 7, The Python Automation Framework – Ansible Basics, and earlier in this chapter: 

$ mkdir host_vars

In our case, we execute the commands on the localhost, and so the file under host_vars should be named accordingly, such as host_vars/localhost. In our host_vars/localhost file, we can also keep the variables declared in group_vars:

$ cat host_vars/localhost
---
"nexus_devices":
"nx-osv-1":
"hostname": "nx-osv-1"
"username": "{{ username }}"
"password": "{{ password }}"
"vlans": [100, 200, 300]
"l3_vlan_interfaces": True
"vlan_interfaces": [
{"int_num": "100", "ip": "192.168.10.1"},
{"int_num": "200", "ip": "192.168.20.1"},
{"int_num": "300", "ip": "192.168.30.1"}
]
"netflow_enable": True

"nx-osv-2":
"hostname": "nx-osv-2"
"username": "{{ username }}"
"password": "{{ password }}"
"vlans": [100, 200, 300]
"l3_vlan_interfaces": False
"netflow_enable": False

After we separate out the variables, the playbook now becomes very lightweight and only consists of the logic of our operation:

    $ cat chapter8_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 also help with securing the files by allowing us to encrypt the sensitive information with Ansible Vault, which we will look at next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.49.182