Skip to content

Instantly share code, notes, and snippets.

@chrisjsewell
Last active December 5, 2020 01:22
Show Gist options
  • Save chrisjsewell/c8b4465e1bc006f483d9313d3a33eff4 to your computer and use it in GitHub Desktop.
Save chrisjsewell/c8b4465e1bc006f483d9313d3a33eff4 to your computer and use it in GitHub Desktop.
ansible to GH actions
# use `# noqa xxx` at the end of a line, to ignore a particular error
# or add to the warn_list, to ignore for the whole project
warn_list: []
*.swp
.DS_Store
.galaxy_install_info
.vscode/
.tox/
# For use with pre-commit.
# See usage instructions at https://pre-commit.com
repos:
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v1.5.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, "2", --preserve-quotes]
- repo: https://github.com/adrienverge/yamllint
rev: v1.25.0
hooks:
- id: yamllint
- repo: https://github.com/ansible/ansible-lint
rev: v4.3.5
hooks:
- id: ansible-lint
extends: default
rules:
document-start: disable
line-length:
max: 200
level: warning
indentation:
spaces: 2
indent-sequences: false

[![CI](https://github.com/marvel-nccr/ansible-role-{{ name }}/workflows/CI/badge.svg)](https://github.com/marvel-nccr/ansible-role-{{ name }}/actions) [![Ansible Role](https://img.shields.io/ansible/role/{{ id }}.svg)](https://galaxy.ansible.com/marvel-nccr/{{ name }}) [![Release](https://img.shields.io/github/tag/marvel-nccr/ansible-role-{{ name }}.svg)](https://github.com/marvel-nccr/ansible-role-{{ name }}/releases)

Ansible Role: marvel-nccr.{{ name }}

{{ description }}

Installation

ansible-galaxy install marvel-nccr.{{ name }}

Role Variables

See defaults/main.yml

Example Playbook

- hosts: servers
  roles:
  - role: marvel-nccr.{{ name }}

Development and testing

This role uses Molecule and Docker for tests.

After installing Docker:

Clone the repository into a package named marvel-nccr.{{ name }} (the folder must be named the same as the Ansible Galaxy name)

git clone https://github.com/marvel-nccr/ansible-role-{{ name }} marvel-nccr.{{ name }}
cd marvel-nccr.{{ name }}

Then run:

pip install -r requirements.txt  # Installs molecule
molecule test  # runs tests

or use tox (see tox.ini):

pip install tox
tox

Code style

Code style is formatted and linted with pre-commit.

pip install pre-commit
pre-commit run -all

Deployment

Deployment to Ansible Galaxy is automated via GitHub Actions. Simply tag a release vX.Y.Z to initiate the CI and release workflow. Note, the release will only complete if the CI tests pass.

License

MIT

Contact

Please direct inquiries regarding Quantum Mobile and associated ansible roles to the AiiDA mailinglist.

# configuration to run via tox
# to use a specific docker image: `MOLECULE_DISTRO=ubuntu1804 tox`
# one of: ubuntu1804, ubuntu2004, centos8, fedora31
[tox]
envlist = molecule
[testenv]
skip_install = true
basepython = python3
[testenv:molecule]
deps = -rrequirements.txt
passenv =
HOME
MOLECULE_DISTRO
MOLECULE_DOCKER_COMMAND
commands = molecule {posargs:test}
name: CI
on:
push:
branches: [master]
tags:
- 'v*'
pull_request:
env:
galaxy-name: "marvel-nccr.{{ name }}"
{% raw %}
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8
- uses: pre-commit/action@v2.0.0
molecule:
strategy:
matrix:
distro: [ubuntu1604, ubuntu1804, ubuntu2004, centos8, fedora31]
fail-fast: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
path: ${{ env.galaxy-name }}
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Upgrade pip
run: |
pip install --upgrade pip
pip --version
- name: Install requirements
run: |
pip install wheel
pip install -r requirements.txt
working-directory: ${{ env.galaxy-name }}
- name: Run molecule
run: molecule test
working-directory: ${{ env.galaxy-name }}
env:
MOLECULE_DISTRO: ${{ matrix.distro }}
release:
name: Publish to ansible-galaxy
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
needs: [pre-commit, molecule]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: robertdebock/galaxy-action@1.0.3
with:
galaxy_api_key: ${{ secrets.GALAXY_API_KEY }}
{% endraw %}
dependency:
name: galaxy
# requirements_file: molecule/default/requirements.yml
driver:
name: docker
platforms:
- name: instance
image: "marvelnccr/docker-${MOLECULE_DISTRO:-ubuntu1804}-ansible:latest"
# by default the container will initialise with systemd as PID1
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
env:
LC_ALL: "en_US.UTF-8"
LANG: "en_US.UTF-8"
provisioner:
name: ansible
# log: true # use for debugging
playbooks:
converge: converge.yml
config_options:
defaults:
# nicer stdout printing
stdout_callback: yaml
bin_ansible_callbacks: true
# add timing to tasks
callback_whitelist: profile_tasks
# for running
ansible~=2.10.0
# for testing
molecule[docker]~=3.1.0
docker~=4.2
[tox]
envlist = run
basepython = python3
[testenv:run]
skip_install = true
deps =
click
jinja2
black
commands = python write-to-repo.py {posargs}
[testenv:role]
skip_install = true
deps =
ansible
commands = ansible-galaxy role info marvel-nccr.{posargs:aiida}
from pathlib import Path
import click
from jinja2 import Environment, StrictUndefined
from jinja2.exceptions import UndefinedError
this_dir = Path(__file__).parent
files = {
".ansible-lint": ".ansible-lint",
".yamllint.yml": ".yamllint.yml",
".gitignore": ".gitignore",
".pre-commit-config.yaml": ".pre-commit-config.yaml",
"README.md": "README.md",
"_tox.ini": "tox.ini",
"ci.yml": ".github/workflows/ci.yml",
"molecule.yml": "molecule/default/molecule.yml",
"requirements.txt": "requirements.txt",
}
@click.command()
@click.argument(
"path",
type=click.Path(exists=True, file_okay=False, resolve_path=True),
)
@click.argument("name")
@click.argument("role-id", type=int)
@click.option("-d", "--description", default="Description...")
def cli(path, name, role_id, description):
"""
name = marvel-nccr.{name}
role-id = run `tox -e role {name}
"""
path = Path(path)
env = Environment(undefined=StrictUndefined)
for in_file, out_file in files.items():
click.echo(f"{in_file} -> {out_file}")
template = env.from_string((this_dir / in_file).read_text())
try:
text = template.render(name=name, id=role_id, description=description)
except UndefinedError as error:
click.echo(f"Missing variable: {error}", nl="red")
break
out_path = path / out_file
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(text.rstrip() + "\n")
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment