28 May

Referencing Jinja2 dynamic variables

Just a nice trick. Let’s assume we are using a CSV file and also a YAML file to feed input data to python template.render() function.

In the CSV file, one row is a single Node we would like to generate configuration for. However, some type of variables is easier to maintain in a YAML file. Think about how nice hierarchy we can create.

To connect these two together on the template level we can do something like this:

Our input YAML file (globals)

nodes:
	TESTNODE:
		hostname: cisco

Our input CSV file (node_csv):

INDEX,NODE_NAME,NODE_ID,SITE_ID
10,TESTNODE,7,163

Python template render function:

template.render(node_csv=node_csv,globals=globals)

In Jinja2 we can use the following :

{% set node_name = node_csv.NODE_NAME %}

hostname {{ globals.nodes.get(node_name).hostname }}

The point here is to use the get function to reference a Jinja2 variable inside a variable.

08 May

Package control installation behind proxy

Sublime3 text editor is one of the best Editor I have seen, and using it for a while. It is beautiful and also easy to extend. There are a number of packages I use, so the first step after an installation is to setup package-manager.

If you are behind a proxy, the following snipet can help. •

import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler({"http":"http://[proxy_username]:[proxy_password]@[proxy_IP_or_host]:[proxy_port]"})) ); by = urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by);

Don’t forget to check the actual hash, and update in the “h” variable and also the proxy settings:)

https://packagecontrol.io/installation

What packages I’m using?

{
	"bootstrapped": true,
	"in_process_packages":
	[
	],
	"installed_packages":
	[
		"Ansible",
		"Dockerfile Syntax Highlighting",
		"EasyDiff",
		"FileDiffs",
		"Generate Password",
		"GenerateUUID",
		"Jinja2",
		"JSONLint",
		"JsonTree",
		"Network Tech",
		"Package Control",
		"Pretty JSON",
		"Scratchpad",
		"SFTP",
		"TodoReview"
	]
}
03 Apr

Python based automation framework: Nornir

Nornir is an automation framework written in python to be used with python. Most automation frameworks hide the language they are written in by using some cumbersome pseudo-language which usually is almost Turing complete but lacks tooling to debug and troubleshoot. Integrating with other systems is also usually quite hard as they usually have complex APIs if any at all. Some of the other common problems of those pseudo-languages is that are usually quite bad at dealing with data and re-usability is limited.
This project might be a good alternative to Ansible, specially if the project is Python based or related to a Python project. More info: https://nornir.readthedocs.io/en/stable/index.html