On the same theme as the previous post, I want to show another snippet of code in Python, this time, to “lint” the Yaml file, this should also be very valid in the Merge Request, forcing a quality of Blueprints.

What is Lint? Lint is the computer science term for a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructions.

There are Lints for different types of Scripts, Languages, files, etc. You will notice that I will use this resource a lot when building Pipelines.

For this code, I will use the Python module yamllint https://pypi.org/project/yamllint/ , see that the website has different modes of use and configurations. There are ways to suppress some types of errors, warnings, etc. In the example, I won’t remove anything, leaving it 100% default. I leave it to you to analyze what is important to interrupt something or not.

I will again use the same YAML from the initial version of the DNS Blueprint, in this example below there are 4 errors and 1 warning.

version: 1
group: infrastructure
tech: dns 
service: devops-db.info
description: Blueprint with DNS zones from devops-db.info
nameservers:
  - name: ns1.devops-db.info
    class: IN
    type: A    
    destination: 172.21.5.72
admin: admin.devops-db.info
serial_number: 2022122800
time_to_refresh: 12h
time_to_retry: 15m
time_to_expire: 3w
minimum_ttl: 2h
zones:
    - host: ldap
      class: IN
      type: A
      destination: 172.21.5.150
    - host: registry
      class: IN
      type: A
      destination: 172.21.5.15

The script below loads the YamlLintConfig Configs and applies them to a Yaml file, the result I transfer to a List of dictionaries, so I can work better than a Generator type.

The result is just a List that I print in a format similar to the previous post, generating an Exception is in accordance with the need for implementation in the future.

# https://pypi.org/project/yamllint/

from yamllint.config import YamlLintConfig
from yamllint import linter
from datetime import datetime

str_DateTime = ''

yaml_config = YamlLintConfig('extends: default')
obj_YamlLinter = linter.run(open('devops-db.info.yaml', "r"), yaml_config)

str_DateTime = datetime.now().strftime('%H:%M:%S')

lst_YamlLinter = []

for p in obj_YamlLinter:
    lst_YamlLinter.append({'level': p.level, 'desc': p.desc, 'line': p.line})

if len(lst_YamlLinter) == 0:
    print(str_DateTime + ' - Blueprint OK.')
else:
    for p in lst_YamlLinter:
        print(str_DateTime + ' - Blueprint linter validation ' + str(p['level']) + ' / Reason: ' + str(p['desc']) + ' / Where: Line ' + str(p['line']))

The result for the YAML above should be:

14:39:37 - Blueprint linter validation warning / Reason: missing document start "---" / Where: Line 1
14:39:37 - Blueprint linter validation error / Reason: trailing spaces / Where: Line 3
14:39:37 - Blueprint linter validation error / Reason: trailing spaces / Where: Line 9
14:39:37 - Blueprint linter validation error / Reason: wrong indentation: expected 2 but found 4 / Where: Line 18
14:39:37 - Blueprint linter validation error / Reason: too many blank lines (1 > 0) / Where: Line 26

Fixing the above errors, I run it again and the result is:

14:47:47 - Blueprint OK.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.