Created
June 2, 2020 02:54
-
-
Save relrod/4165c7a4ed32947dbfd19961c1c86b4f to your computer and use it in GitHub Desktop.
Silly morse code inventory plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A super simple morse-code inventory plugin for demonstration and learning | |
# purposes. | |
# | |
# It takes files with lines like this: | |
# ! -- -.-- ..--.- --. .-. --- ..- .--. // the "my_group" group | |
# .- -.. -.-. ----- .---- ansible_user=nonroot // and some comment here | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
from ansible.inventory.group import to_safe_group_name | |
from ansible.plugins.inventory import BaseFileInventoryPlugin | |
from ansible.module_utils._text import to_bytes, to_text | |
import itertools | |
morse = { | |
".-": "a", "-...": "b", "-.-.": "c", "-..": "d", ".": "e", "..-.": "f", | |
"--.": "g", "....": "h", "..": "i", ".---": "j", "-.-": "k", ".-..": "l", | |
"--": "m", "-.": "n", "---": "o", ".--.": "p", "--.-": "q", ".-.": "r", | |
"...": "s", "-": "t", "..-": "u", "...-": "v", ".--": "w", "-..-": "x", | |
"-.--": "y", "--..": "z", | |
".-.-.-": ".", "..--.-": "_", "-....-": "-", | |
"-----": "0", ".----": "1", "..---": "2", "...--": "3", "....-": "4", | |
".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9", | |
} | |
def decode_morse(morse_str): | |
return ''.join(morse[s] for s in morse_str.split()) | |
class InventoryModule(BaseFileInventoryPlugin): | |
""" | |
Takes a morse code inventory file and builds a list of hosts and groups. | |
Subgroups, caching, and host patterns are left as an exercise to the very | |
bored or very curious reader. | |
""" | |
NAME = 'morse' | |
def parse(self, inventory, loader, path, cache=True): | |
""" | |
Figure out if the line is introducing a group, a host, or a comment, | |
then if necessary add it to the InventoryData. | |
""" | |
super(InventoryModule, self).parse(inventory, loader, path) | |
# We assume there is a DataLoader | |
(data, private) = self.loader._get_file_contents(path) | |
data = to_text(data) | |
lines = data.splitlines() | |
# Default group if we're only given a list of hosts | |
groupname = 'ungrouped' | |
for line in lines: | |
# Strip comments | |
line = line.strip().split('//', 1)[0] | |
# If it's a comment line, ignore it and move on. | |
if not line: | |
continue | |
# If it starts with !, it's a group. | |
if line.startswith('!'): | |
groupname = to_safe_group_name(decode_morse(line[1:].strip())) | |
self.inventory.add_group(groupname) | |
continue | |
# Otherwise it's a host. | |
# Yes, there are more efficient ways to do this. | |
host = itertools.takewhile(lambda x: x in ('-', '.', ' '), line) | |
host = decode_morse(''.join(host)) | |
vrs = itertools.dropwhile(lambda x: x in ('-', '.', ' '), line) | |
vrs = ''.join(vrs).split() | |
vrs_hash = {} | |
for var in vrs: | |
(k, v) = var.split('=', 1) | |
# See how the built-in 'ini' inventory plugin uses 'ast' for an | |
# example of how to coerce values into Python types. Here we | |
# just keep strings. | |
vrs_hash[k] = v | |
self._populate_host_vars([host], vrs_hash, groupname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment