diff --git a/Dockerfile b/Dockerfile index d2578e7..cd106fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" diff --git a/README.md b/README.md index 325b8d2..908a0f1 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -49,7 +74,7 @@ on: or on various [events](https://help.github.com/en/articles/events-that-trigger-workflows) -
+
Contributing ------------- @@ -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).
-It was used by ansible for lint checks. at [ansible/ansible-lint-action](https://github.com/ansible/ansible-lint-action.git)
-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)
+It was modified from [L3D](github.com/do1jlr) to check ansible roles. diff --git a/action.yml b/action.yml index 4bbe160..ae53d42 100644 --- a/action.yml +++ b/action.yml @@ -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 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' diff --git a/ansible-docker.sh b/ansible-docker.sh index 067ce8c..061ca3f 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -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] @@ -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