Skip to content

Commit

Permalink
Merge pull request #1 from roles-ansible/playbook
Browse files Browse the repository at this point in the history
add supports for playbooks
  • Loading branch information
DO1JLR authored Feb 20, 2020
2 parents d4776ca + 6201739 commit cbd0678
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LABEL "repository"="https://github.com/roles-ansible/check-ansible-debian-stable
LABEL "homepage"="https://github.com/roles-ansible/check-ansible-debian-stable-action"

LABEL "com.github.actions.name"="check-ansible-debian-stable"
LABEL "com.github.actions.description"="Check ansible role with Debian stable"
LABEL "com.github.actions.description"="Check ansible role or playbook with Debian stable"
LABEL "com.github.actions.icon"="aperture"
LABEL "com.github.actions.color"="green"

Expand Down
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

Check Ansible Debian stable
=======================
This action allows you to test your ansible role in a Docker Container with ``debian:stable``.
This action allows you to test your ansible role or your playbook in a Docker Container with ``debian:stable``.

## Usage
To use the action simply create an `ansible-debian-stable.yml` (or choose custom `*.yml` name) in the `.github/workflows/` directory.
To use the action simply create an ``ansible-debian-stable.yml`` *(or choose custom ``*.yml`` name)* in the ``.github/workflows/`` directory.

For example:

Expand All @@ -28,12 +28,37 @@ jobs:
# replace "master" with any valid ref
uses: roles-ansible/check-ansible-debian-stable-action@master
with:
# [required]
# Paths to your ansible role you want to test
# For Example:
# targets: "role/my_role/"
targets: "./"

# [required]
# Paths to your ansible role or playboox.yml you want to test
# Some Examples:
# targets: "role/my_role/"
# targets: "site.yml"
#
# group: ""
# [optional]
# When testing playbooks you have to tell ansible
# the group you that we write in our hosts file.
# example:
# group: 'servers'
# hosts: ""
# [optional]
# When testing playbooks you have to give one example
# host this action should use to test your playbook.
# > We only spawn one docker container that mean
# > we can only test one host at the time. Sorry
# some examples:
# hosts: 'localhost'
# hosts: 'srv01.example.com'
# requirements
# [optional]
# When testing playbooks and you are using ansible galaxy,
# you may be interested in installing your requirements
# from ansible galaxy.
# To do this please provide us either the role name directly
# requirements: 'do1jlr.ansible_version'
# or your requiements.yml file.
# requirements: 'requirements.yml'
```

Alternatively, you can run the ansible check only on certain branches:
Expand All @@ -49,7 +74,7 @@ on:
or on various [events](https://help.github.com/en/articles/events-that-trigger-workflows)
<br>
<br/>
Contributing
-------------
Expand All @@ -63,5 +88,5 @@ The Dockerfile and associated scripts and documentation in this project are rele
--------------
The initial GitHub action has been created by [Stefan Stölzle](/stoe) at
[stoe/actions](https://github.com/stoe/actions).<br/>
It was used by ansible for lint checks. at [ansible/ansible-lint-action](https://github.com/ansible/ansible-lint-action.git)<br/>
It was modified from L3D to check ansible roles.
It was used by ansible for lint checks at [ansible/ansible-lint-action](https://github.com/ansible/ansible-lint-action.git)<br/>
It was modified from [L3D](github.com/do1jlr) to check ansible roles.
34 changes: 31 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
name: Check Ansible Debian stable
description: Check ansible role with the Debian stable docker container
description: Check ansible role or playbook with the debian:stable docker container
author: L3D <[email protected]>

inputs:
targets:
description: |
Paths to the ansible role you want to be tested.
For example './' or 'roles/my_role/'
Paths to the ansible role or playbook you want to be tested.
For example './', 'roles/my_role/' or 'site.yml'
required: true
group:
description: |
When testing playbooks you have to tell ansible
the group you that we write in our hosts file.
For example 'servers' or what group you are using
in the playbook you want to test!
required: false
hosts:
description: |
When testing playbooks you have to give us
one example host we should use to test your playbook.
For example 'server1.example.com'.
We only spawn one docker container that mean
we can only test one host at the time. Sorry
required: false
requirements:
description: |
When testing playbooks and you are using ansible galaxy,
you may be interested in installing your requirements
from ansible galaxy.
To do this please provide us either the role name directly
-> e.g. 'do1jlr.ansible_version'
or your requiements.yml file.
-> e.g. 'requirements.yml'
required: false
runs:
using: docker
image: Dockerfile
env:
TARGETS: ${{ inputs.targets }}
HOSTS: ${{ inputs.hosts }}
GROUP: ${{ inputs.group }}
REQUIREMENTS: ${{ inputs.requirements }}
branding:
icon: 'aperture'
color: 'green'
66 changes: 51 additions & 15 deletions ansible-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ set -x

# Generates client.
# env:
# [required] TARGETS : Path to your ansible role you want to be tested. (e.g, './' or 'roles/my_role/') to be tested
ansible::test() {
# [required] TARGETS : Path to your ansible role or to a playbook .yml file you want to be tested.
# (e.g, './' or 'roles/my_role/' for roles or 'site.yml' for playbooks)


ansible::prepare() {
: "${TARGETS?No targets to check. Nothing to do.}"
: "${GITHUB_WORKSPACE?GITHUB_WORKSPACE has to be set. Did you use the actions/checkout action?}"
pushd ${GITHUB_WORKSPACE}

# generate playbook to be executed
echo -e """---
- name: test a ansible role
hosts: localhost
tags: default
roles:
- \""${TARGETS}"\"
""" | tee -a deploy.yml

# generate ansible.cfg
echo -e """
[defaults]
Expand All @@ -33,16 +27,58 @@ fact_caching_timeout = 7200
stdout_callback = yaml
ansible_python_interpreter=/usr/bin/python3
ansible_connection=local
""" | tee -a ansible.cfg
""" | tee ansible.cfg

# create host list
echo -e "[local]\nlocalhost" | tee -a host.ini
echo -e "[local]\nlocalhost ansible_python_interpreter=/usr/bin/python3 ansible_connection=local" | tee host.ini
}
ansible::test::role() {
: "${TARGETS?No targets to check. Nothing to do.}"
: "${GITHUB_WORKSPACE?GITHUB_WORKSPACE has to be set. Did you use the actions/checkout action?}"
pushd ${GITHUB_WORKSPACE}

# generate playbook to be executed
echo -e """---
- name: test a ansible role
hosts: localhost
tags: default
roles:
- \""${TARGETS}"\"
""" | tee -a deploy.yml

# execute playbook
ansible-playbook -vvv -i localhost deploy.yml
ansible-playbook --connection=local --limit localhost deploy.yml
}
ansible::test::playbook() {
: "${TARGETS?No targets to check. Nothing to do.}"
: "${GITHUB_WORKSPACE?GITHUB_WORKSPACE has to be set. Did you use the actions/checkout action?}"
: "${HOSTS?at least one valid host is required to check your playbook!}"
: "${GROUP?Please define the group your playbook is written for!}"
pushd ${GITHUB_WORKSPACE}

echo -e "[${GROUP}]\n${HOSTS} ansible_python_interpreter=/usr/bin/python3 ansible_connection=local ansible_host=127.0.0.1" | tee host.ini

# execute playbook
ansible-playbook --connection=local --inventory host.ini ${TARGETS}
}

# make sure git is up to date
git submodule update --init --recursive
if [[ "${REQUIREMENTS}" == *.yml ]]
then
ansible-galaxy install -r ${REQUIREMENTS}
else
[ ! -z "${REQUIREMENTS}" ] && ansible-galaxy install ${REQUIREMENTS}
fi
if [ "$0" = "$BASH_SOURCE" ] ; then
>&2 echo -E "\nRunning Ansible debian check...\n"
ansible::test
ansible::prepare
if [[ "${TARGETS}" == *.yml ]]
then
echo -e "\nansible playbook detected\ninitialize playbook testing...\n"
ansible::test::playbook
else
echo -e "\nno playbook detected\ninitialize role testing...\n"
ansible::test::role
fi
fi

0 comments on commit cbd0678

Please sign in to comment.