From 112f4a3c2795348640837e29f5c29c72625a39a9 Mon Sep 17 00:00:00 2001 From: diademiemi Date: Fri, 26 Jan 2024 19:26:22 +0100 Subject: [PATCH] Initial commit --- .ansible-lint | 12 ++ .../ansible-role-template.yml | 50 ++++++ .../workflow-templates/copy-role-template.sh | 11 ++ .github/workflows/galaxy-import.yml | 58 +++++++ .gitignore | 151 ++++++++++++++++++ .pre-commit-config.yaml | 30 ++++ CHANGELOG.rst | 0 LICENSE | 21 +++ README.md | 48 ++++++ add-role.sh | 22 +++ ...lection_diademiemi.template.code-workspace | 35 ++++ changelogs/config.yaml | 32 ++++ changelogs/fragments/.gitkeep | 0 docs/.gitkeep | 0 galaxy.yml | 72 +++++++++ meta/runtime.yml | 52 ++++++ plugins/README.md | 31 ++++ replace.sh | 35 ++++ requirements.yml | 5 + roles/.gitkeep | 0 20 files changed, 665 insertions(+) create mode 100644 .ansible-lint create mode 100644 .github/workflow-templates/ansible-role-template.yml create mode 100755 .github/workflow-templates/copy-role-template.sh create mode 100644 .github/workflows/galaxy-import.yml create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 CHANGELOG.rst create mode 100644 LICENSE create mode 100644 README.md create mode 100755 add-role.sh create mode 100644 ansible_collection_diademiemi.template.code-workspace create mode 100644 changelogs/config.yaml create mode 100644 changelogs/fragments/.gitkeep create mode 100644 docs/.gitkeep create mode 100644 galaxy.yml create mode 100644 meta/runtime.yml create mode 100644 plugins/README.md create mode 100755 replace.sh create mode 100644 requirements.yml create mode 100644 roles/.gitkeep diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..a221a4b --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,12 @@ +--- +skip_list: + - "var-naming[no-role-prefix]" # Allow _ and __ internal vars + - run-once[task] # This check is broken + +# Exclude downloaded roles and collections from linting +exclude_paths: + - collections/** + - roles/** + - changelogs + +... diff --git a/.github/workflow-templates/ansible-role-template.yml b/.github/workflow-templates/ansible-role-template.yml new file mode 100644 index 0000000..8e9728c --- /dev/null +++ b/.github/workflow-templates/ansible-role-template.yml @@ -0,0 +1,50 @@ +--- +# This workflow requires a GALAXY_API_KEY secret present in the GitHub +# repository or organization. + +name: CI + +"on": + push: + branches: + - main + paths: + - "roles/template/**" + workflow_dispatch: + +jobs: + molecule: + runs-on: ubuntu-20.04 + name: Molecule test "template" role + strategy: + fail-fast: false + steps: + - name: Checkout + uses: actions/checkout@v3 + + # - name: "Free Disk Space." + # uses: jlumbroso/free-disk-space@main + # with: + # tool-cache: true + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install dependencies. + working-directory: roles/template + run: pip3 install -r requirements.txt + + - name: Install Ansible dependencies. + run: ansible-galaxy install -r requirements.yml + + - name: Install Ansible dependencies specific to role. + working-directory: roles/template + run: ansible-galaxy install -r requirements.yml + + - name: Run molecule + working-directory: roles/template + run: "molecule test" + +... diff --git a/.github/workflow-templates/copy-role-template.sh b/.github/workflow-templates/copy-role-template.sh new file mode 100755 index 0000000..aa9661a --- /dev/null +++ b/.github/workflow-templates/copy-role-template.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Run this relative to the collection root +# ./copy-role-template.sh example + +export role_name=$1 + +cp .github/workflow-templates/ansible-role-template.yml .github/workflows/ansible-role-$role_name.yml + +sed -i "s/template/$role_name/g" .github/workflows/ansible-role-$role_name.yml + +echo "Workflow for $role_name created" \ No newline at end of file diff --git a/.github/workflows/galaxy-import.yml b/.github/workflows/galaxy-import.yml new file mode 100644 index 0000000..dd42c79 --- /dev/null +++ b/.github/workflows/galaxy-import.yml @@ -0,0 +1,58 @@ +--- +# This workflow requires a GALAXY_API_KEY secret present in the GitHub +# repository or organization. + +name: Import to Galaxy +'on': + push: + branches: + - main + paths: + - "galaxy.yml" + workflow_dispatch: + +permissions: + contents: write + +jobs: + galaxy-import: + name: Import to Galaxy + runs-on: ubuntu-latest + steps: + - name: Check out the codebase. + uses: actions/checkout@v3 + + - name: Get version from "galaxy.yml". + id: version + run: echo "collection_version=$(yq '.version' galaxy.yml)" >> "$GITHUB_ENV" + + - name: Check if tag already exists + uses: mukunku/tag-exists-action@v1.2.0 + id: check-tag + with: + tag: '${{ env.collection_version }}' + + - name: Set up Python 3. + if: "${{ steps.check-tag.outputs.exists == 'false' }}" + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install Ansible. + if: "${{ steps.check-tag.outputs.exists == 'false' }}" + run: pip3 install ansible-core + + - name: Trigger a new import on Galaxy. + if: "${{ steps.check-tag.outputs.exists == 'false' }}" + run: >- + ansible-galaxy collection build && + ansible-galaxy collection publish --api-key ${{ secrets.GALAXY_API_KEY }} $(ls -t *.tar.gz | head -n 1) + + - name: Add tag. + if: "${{ steps.check-tag.outputs.exists == 'false' }}" + uses: anothrNick/github-tag-action@1.61.0 # Don't use @master unless you're happy to test the latest version + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CUSTOM_TAG: ${{ env.collection_version }} + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b796ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,151 @@ +# Editors +.idea/ +.vscode/ + +# Credentials +**/kubeconfig.yaml +.*vault* + +# Terraform +**/.terraform/* +*.tfstate +*.tfstate.* +.terraform.lock.hcl + +# Ansible +/tests/output/ +/changelogs/.plugin-cache.yaml +output/ +.collection_root/* + +**/collections/** +!**/collections/requirements.yml +!/**/.gitkeep + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.venv +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..dc43d3d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,30 @@ +--- +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: trailing-whitespace + args: [--markdown-linebreak-ext=md] + - id: end-of-file-fixer + - id: check-added-large-files + - id: check-yaml + files: .*\.(yaml|yml)$ + args: [--unsafe] + - id: check-json + - id: check-xml + - id: check-toml + - id: check-case-conflict + - id: check-merge-conflict + - id: detect-private-key + - id: check-shebang-scripts-are-executable + - id: check-executables-have-shebangs + - id: check-added-large-files + + - repo: https://github.com/ansible/ansible-lint + rev: v6.17.0 + hooks: + - id: ansible-lint + files: \.(yaml|yml)$ + entry: sh -c 'python3 -m pip install -r requirements.txt; python3 -m ansiblelint -v --force-color' + +... diff --git a/CHANGELOG.rst b/CHANGELOG.rst new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5defc68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 diademiemi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d9c311 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +Ansible Collection - diademiemi.template +======================================== +Documentation for the collection template. + +Contents +======== + +Roles +------ +Role | Description | CI Status +--- | --- | --- + + +Click on the role to see the README for that role. + +Collection Structure +-------------- + +This collection makes use of my [Ansible Role Template repository](https://github.com/diademiemi/ansible_role_%74emplate.git). The `add-role.sh` script downloads this Template and generates a new role with the name specified. If a `molecule/default/molecule.yml` file is present, it will be ran with GitHub Actions. + + +Usage: +```bash +export NEW_ROLE_NAME="new_role" +./add-role.sh ${NEW_ROLE_NAME} +``` + +Using Template +-------------- +To use this template for a new role, run + + +```bash +export NEW_ROLE_NAME="NEW_NAME" +export GITHUB_USER="diademiemi" +export GALAXY_API_KEY="YOUR_API_KEY" + +find . -type f -exec sed -i "s/diademiemi/${GITHUB_USER}/g" {} + # Do not run this more than once +find . -type f -exec sed -i "s/template/${NEW_ROLE_NAME}/g" {} + # Do not run this more than once + +# Assumes repo is named ansible_role_${NEW_ROLE_NAME} +gh secret set GALAXY_API_KEY -R ${GITHUB_USER}/ansible_collection_${GITHUB_USER}.${NEW_COLLECTION_NAME} -a actions -b ${GALAXY_API_KEY} + +# Remove this section from README.md +sed -i "/Using Template/Q" README.md +``` + +This is also provided as a script as `replace.sh`. diff --git a/add-role.sh b/add-role.sh new file mode 100755 index 0000000..3dcd449 --- /dev/null +++ b/add-role.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +echo "Enter role name: " +read role_name + +mkdir -p ./roles/$role_name + +wget -O /tmp/ansible_role_template.$$ https://github.com/diademiemi/ansible_role_template/archive/refs/heads/main.zip + +./.github/workflow-templates/copy-role-template.sh $role_name + +cd ./roles/$role_name + +ln -s . ansible_role_template-main +unzip /tmp/ansible_role_template.$$ +rm ansible_role_template-main + +rm /tmp/ansible_role_template.$$ + +ROLE_IN_COLLECTION="true" bash replace.sh $role_name + +echo "Role $role_name created" \ No newline at end of file diff --git a/ansible_collection_diademiemi.template.code-workspace b/ansible_collection_diademiemi.template.code-workspace new file mode 100644 index 0000000..b80870f --- /dev/null +++ b/ansible_collection_diademiemi.template.code-workspace @@ -0,0 +1,35 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "ansible.python.interpreterPath": "/bin/python3", + "files.associations": { + "**/tasks/**/*.yml": "ansible", + "**/vars/**/*.yml": "ansible", + "**/molecule/*.yml": "ansible", + "**/molecule/molecule.yml": "ansible", + "**/handlers/**/*.yml": "ansible", + "**/defaults/*.yml": "ansible", + "**/host_vars/**/*.yml": "ansible", + "**/group_vars/**/*.yml": "ansible", + "**/inventories/*.yml": "ansible", + "**/meta/*.yml": "ansible", + "**/requirements.yml": "ansible", + "**/terraform/**": "terraform", + "**/inventories/terraform/**": "ansible" + } + }, + "extensions": { + "recommendations": [ + "redhat.ansible", + "redhat.vscode-yaml", + "hashicorp.terraform", + "GitHub.copilot-nightly", + "GitLab.gitlab-workflow", + "yzhang.markdown-all-in-one" + ] + } +} \ No newline at end of file diff --git a/changelogs/config.yaml b/changelogs/config.yaml new file mode 100644 index 0000000..5759ef4 --- /dev/null +++ b/changelogs/config.yaml @@ -0,0 +1,32 @@ +changelog_filename_template: ../CHANGELOG.rst +changelog_filename_version_depth: 0 +changes_file: changelog.yaml +changes_format: combined +ignore_other_fragment_extensions: true +keep_fragments: false +mention_ancestor: true +new_plugins_after_name: removed_features +notesdir: fragments +prelude_section_name: release_summary +prelude_section_title: Release Summary +sanitize_changelog: true +sections: + - - major_changes + - Major Changes + - - minor_changes + - Minor Changes + - - breaking_changes + - Breaking Changes / Porting Guide + - - deprecated_features + - Deprecated Features + - - removed_features + - Removed Features (previously deprecated) + - - security_fixes + - Security Fixes + - - bugfixes + - Bugfixes + - - known_issues + - Known Issues +title: diademiemi.template +trivial_section_name: trivial +use_fqcn: true diff --git a/changelogs/fragments/.gitkeep b/changelogs/fragments/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docs/.gitkeep b/docs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..b868b43 --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,72 @@ +### REQUIRED +# The namespace of the collection. This can be a company/brand/organization or product namespace under which all +# content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with +# underscores or numbers and cannot contain consecutive underscores +namespace: diademiemi + +# The name of the collection. Has the same character restrictions as 'namespace' +name: template + +# The version of the collection. Must be compatible with semantic versioning +version: 1.0.0 + +# The path to the Markdown (.md) readme file. This path is relative to the root of the collection +readme: README.md + +# A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) +# @nicks:irc/im.site#channel' +authors: + - diademiemi + + +### OPTIONAL but strongly recommended +# A short summary description of the collection +description: your collection description + +# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only +# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' +license: + - MIT + +# The path to the license file for the collection. This path is relative to the root of the collection. This key is +# mutually exclusive with 'license' +# license_file: 'LICENSE' + +# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character +# requirements as 'namespace' and 'name' +tags: + - linux + +# Collections that this collection requires to be installed for it to be usable. The key of the dict is the +# collection label 'namespace.name'. The value is a version range +# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version +# range specifiers can be set and are separated by ',' +dependencies: {} + +# The URL of the originating SCM repository +repository: https://github.com/diademiemi/ansible_collection_diademiemi.template + +# The URL to any online docs +documentation: https://github.com/diademiemi/ansible_collection_diademiemi.template + +# The URL to the homepage of the collection/project +homepage: https://github.com/diademiemi/ansible_collection_diademiemi.template + +# The URL to the collection issue tracker +issues: https://github.com/diademiemi/ansible_collection_diademiemi.template/issues + +# A list of file glob-like patterns used to filter any files or directories that should not be included in the build +# artifact. A pattern is matched from the relative path of the file or directory of the collection directory. This +# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry', +# and '.git' are always filtered. Mutually exclusive with 'manifest' +build_ignore: + - .github + - .gitignore + +# A dict controlling use of manifest directives used in building the collection artifact. The key 'directives' is a +# list of MANIFEST.in style +# L(directives,https://packaging.python.org/en/latest/guides/using-manifest-in/#manifest-in-commands). The key +# 'omit_default_directives' is a boolean that controls whether the default directives are used. Mutually exclusive +# with 'build_ignore' +# manifest: null + diff --git a/meta/runtime.yml b/meta/runtime.yml new file mode 100644 index 0000000..ef61b75 --- /dev/null +++ b/meta/runtime.yml @@ -0,0 +1,52 @@ +--- +# Collections must specify a minimum required ansible version to upload +# to galaxy +requires_ansible: '>=2.11.0' + +# Content that Ansible needs to load from another location or that has +# been deprecated/removed +# plugin_routing: +# action: +# redirected_plugin_name: +# redirect: ns.col.new_location +# deprecated_plugin_name: +# deprecation: +# removal_version: "4.0.0" +# warning_text: | +# See the porting guide on how to update your playbook to +# use ns.col.another_plugin instead. +# removed_plugin_name: +# tombstone: +# removal_version: "2.0.0" +# warning_text: | +# See the porting guide on how to update your playbook to +# use ns.col.another_plugin instead. +# become: +# cache: +# callback: +# cliconf: +# connection: +# doc_fragments: +# filter: +# httpapi: +# inventory: +# lookup: +# module_utils: +# modules: +# netconf: +# shell: +# strategy: +# terminal: +# test: +# vars: + +# Python import statements that Ansible needs to load from another location +# import_redirection: +# ansible_collections.ns.col.plugins.module_utils.old_location: +# redirect: ansible_collections.ns.col.plugins.module_utils.new_location + +# Groups of actions/modules that take a common set of options +# action_groups: +# group_name: +# - module1 +# - module2 diff --git a/plugins/README.md b/plugins/README.md new file mode 100644 index 0000000..34cd30a --- /dev/null +++ b/plugins/README.md @@ -0,0 +1,31 @@ +# Collections Plugins Directory + +This directory can be used to ship various plugins inside an Ansible collection. Each plugin is placed in a folder that +is named after the type of plugin it is in. It can also include the `module_utils` and `modules` directory that +would contain module utils and modules respectively. + +Here is an example directory of the majority of plugins currently supported by Ansible: + +``` +└── plugins + ├── action + ├── become + ├── cache + ├── callback + ├── cliconf + ├── connection + ├── filter + ├── httpapi + ├── inventory + ├── lookup + ├── module_utils + ├── modules + ├── netconf + ├── shell + ├── strategy + ├── terminal + ├── test + └── vars +``` + +A full list of plugin types can be found at [Working With Plugins](https://docs.ansible.com/ansible-core/2.14/plugins/plugins.html). diff --git a/replace.sh b/replace.sh new file mode 100755 index 0000000..0e10567 --- /dev/null +++ b/replace.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +if [ -z "$1" ]; then + echo "Enter collection name: " + read NEW_COLLECTION_NAME +else + NEW_COLLECTION_NAME=$1 +fi + +if [ -z "$2" ]; then + if [ -z "$GITHUB_USER" ]; then + echo "Enter github user: " + read GITHUB_USER + fi +else + GITHUB_USER="$2" +fi + +if [ -z "$GALAXY_API_KEY" ]; then + echo "Enter galaxy api key: " + read GALAXY_API_KEY +fi + +# Assumes repo is named ansible_collection_${GITHUB_USER}.${NEW_ROLE_NAME} +gh secret set GALAXY_API_KEY -R ${GITHUB_USER}/ansible_collection_${GITHUB_USER}.${NEW_COLLECTION_NAME} -a actions -b ${GALAXY_API_KEY} + +find roles docs meta plugins galaxy.yml changelogs LICENSE README.md \ + -type f -exec sed -i -e "s/diademiemi/${GITHUB_USER}/g" -e "s/template/${NEW_COLLECTION_NAME}/g" {} + # Do not run this more than once + +# Remove this section from README.md +sed -i "/Collection Structure/Q" README.md + +rm ./replace.sh + +cd ../../ \ No newline at end of file diff --git a/requirements.yml b/requirements.yml new file mode 100644 index 0000000..91f7a45 --- /dev/null +++ b/requirements.yml @@ -0,0 +1,5 @@ +--- +collections: [] + +roles: [] +... diff --git a/roles/.gitkeep b/roles/.gitkeep new file mode 100644 index 0000000..e69de29