From b34d1ffa3a62337cd1ea44c2c877731bf1b7d7e3 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Fri, 23 Oct 2020 22:18:17 -0400 Subject: [PATCH] Initial commit --- README.md | 81 +++++++++++++++ galaxy.yml | 26 +++++ plugins/modules/armbian_facts.py | 164 +++++++++++++++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 README.md create mode 100644 galaxy.yml create mode 100644 plugins/modules/armbian_facts.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..ffb93f3 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +[![Galaxy](https://img.shields.io/badge/galaxy-samdoran.armbian-blue)](https://galaxy.ansible.com/samdoran/armbian) + + +This collection contains a module for gathering facts on [Armbian](https://www.armbian.com). More Armbian related things may be added in the future. + +**Ansible 2.9 and later is required.** + +The module parses `/etc/armbian-release` and stores the result in `ansible_facts.armbian`. Here is an example: + +``` +"ansible_facts": { + "armbian": { + "arch": "arm", + "board": "orangepiplus2e", + "board_family": "sun8i", + "board_name": "Orange Pi+ 2E", + "board_type": "conf", + "branch": "current", + "build_repository_commit": "e6fa811d-dirty", + "build_repository_url": "https://github.com/armbian/build", + "distribution_codename": "buster", + "distribution_status": "supported", + "image_type": "stable", + "initrd_arch": "arm", + "kernel_image_type": "Image", + "linux_family": "sunxi", + "version": "20.08.1" + } +} +``` + +If `/etc/armbian-release` is not available `ansible_facts.armbian` will be an empty dictionary. It is safe to run this on non-Armbian hosts without causing your playbook to fail. See `ansible-doc samdoran.armbian.armbian_facts` for more details. + +### Installation ### + +`ansible-galaxy collection install samdoran.armbian` + +### Usage ### + +This module can either be called explicitly by a task or added to the list of [`FACTS_MODULES`](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#facts-modules) that run automatically during the fact gathering stage. + +#### Adding to the default Facts Modules #### + +Add the module to the configuration: + +```ini +[default] +facts_modules = samdoran.armbian.armbian_facts +``` + +This means that any time fact gathering is run, `armbian_facts` will be run in addition to the default fact gathering. This can be done in a playbook: + +```yaml +- hosts: armbian + gather_facts: yes +``` + +Or ad hoc: + + ``` + ansible all -m samdoran.armbian.armbian_facts + ansible all -m gather_facts + ``` + +#### Explicit Task #### + +You can call the module at any time using a task. Since this is a facts module, there is no need to register the output. The returned facts will automatically be set for the host. If fact caching is enabled, the gathered facts will be cached. + +``` +- hosts: armbian + gather_facts: yes + + tasks: + - name: Gather Armbian facts + samdoran.armbian.armbian_facts +``` + + +### Modules in this Collection ### + +- `samdoran.armbian.armbian_facts` - Gather facts about Armbian. \ No newline at end of file diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..14747bd --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,26 @@ +namespace: samdoran +name: armbian +version: 1.0.0 +readme: README.md +authors: + - Sam Doran + +description: Roles and custom content for manangig macOS + +license: + - GPL-2.0-or-later + +tags: + - development + - system + - debian + - armbian + +dependencies: {} + +repository: http://github.com/samdoran/ansible-collection-armbian +documentation: http://github.com/samdoran/ansible-collection-armbian/README.md +homepage: http://github.com/samdoran/ansible-collection-armbian +issues: http://github.com/samdoran/ansible-collection-armbian/issues + +build_ignore: [] diff --git a/plugins/modules/armbian_facts.py b/plugins/modules/armbian_facts.py new file mode 100644 index 0000000..0bc87f2 --- /dev/null +++ b/plugins/modules/armbian_facts.py @@ -0,0 +1,164 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (c) 2020 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = """ +--- +module: Armbian Facts +author: + - Sam Doran (@samdoran) +version_added: '1.0.0' +short_description: Collect facts about Armbian +notes: [] +description: + - Gather detailed facts about Armbian from C(/etc/armbian-release). If facts are unable to be gathered, + an empty dictionary is returned. This can be run safelay against non-Armbian hosts. + - This module can be added to the default list of C(FACTS_MODULES). + +options: {} +""" + +EXAMPLES = """ +- armbian_facts: +""" + +RETURN = """ +armbian: + description: Main key containing the Armbian facts. Empty if parsing failed. + returned: always + type: complex + contains: + arch: + description: Architecture + returned: success + type: str + sample: "arm" + board: + description: Board + returned: success + type: str + sample: "helios4" + board_name: + description: Board name + returned: success + type: str + sample: "Helios4" + board_type: + description: Board type + returned: success + type: str + sample: "conf" + board_family: + description: Board family + returned: success + type: str + sample: "mvebu" + branch: + description: Branch + returned: success + type: str + sample: "next" + build_repository_commit: + description: Build repository + returned: success + type: str + sample: "0d21d90f" + build_repsitory_url: + description: Build repository + returned: success + type: str + sample: "https://github.com/armbian/build" + image_type: + description: Image type + returned: success + type: str + sample: "user-built" + initrd_arch: + description: initrd architecture + returned: success + type: str + sample: "arm" + kernel_image_type: + description: Kernel image type + returned: success + type: str + sample: "zImage" + linux_family: + description: Linux family + returned: success + type: str + sample: "mvebu" + version: + description: Armbian version + returned: success + type: str + sample: "5.91" +""" + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.facts.utils import get_file_lines + +# Eample of /etc/armbian-release: +# +# BOARD=helios4 +# BOARD_NAME="Helios4" +# BOARDFAMILY=mvebu +# BUILD_REPOSITORY_URL=https://github.com/armbian/build +# BUILD_REPOSITORY_COMMIT=0d21d90f +# VERSION=5.91 +# LINUXFAMILY=mvebu +# BRANCH=next +# ARCH=arm +# IMAGE_TYPE=user-built +# BOARD_TYPE=conf +# INITRD_ARCH=arm +# KERNEL_IMAGE_TYPE=zImage + + +KEY_TRANSFORMATION = { + 'LINUXFAMILY': 'linux_family', + 'BOARDFAMILY': 'board_family', +} + + +def parse_armbian_release(): + release_file = '/etc/armbian-release' + + lines = get_file_lines(release_file) + + parsed = {} + for line in lines: + if line.startswith('#'): + continue + + try: + key, value = line.split('=', 1) + except ValueError: + continue + + key = KEY_TRANSFORMATION.get(key, key) + parsed[key.lower()] = value.strip('\'"') + + return parsed + + +def main(): + module = AnsibleModule( + argument_spec={ + 'fact_path': {'type': 'path'}, + }, + supports_check_mode=True, + ) + + parsed = parse_armbian_release() + results = {'ansible_facts': {'armbian': parsed}} + module.exit_json(**results) + + +if __name__ == '__main__': + main()