From 1b85abe51437d2ffba2a99f5ef1e83e136e62ba8 Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 10:38:09 +0100 Subject: [PATCH 1/8] Update docs notice that we support playbooks now (with the next commit) --- Dockerfile | 2 +- README.md | 18 +++++++++--------- action.yml | 6 +++--- 3 files changed, 13 insertions(+), 13 deletions(-) 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..13f0f14 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,12 @@ jobs: # replace "master" with any valid ref uses: roles-ansible/check-ansible-debian-stable-action@master with: + targets: "./" # [required] - # Paths to your ansible role you want to test - # For Example: + # Paths to your ansible role or playboox.yml you want to test + # Some Examples: # targets: "role/my_role/" - targets: "./" - + # targets: "site.yml" ``` Alternatively, you can run the ansible check only on certain branches: @@ -49,7 +49,7 @@ on: or on various [events](https://help.github.com/en/articles/events-that-trigger-workflows) -
+
Contributing ------------- @@ -63,5 +63,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..f43cbc6 100644 --- a/action.yml +++ b/action.yml @@ -1,12 +1,12 @@ 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 runs: using: docker From 636b073a475e01c42d040d580488895d479c795d Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 10:39:25 +0100 Subject: [PATCH 2/8] Support playbook checks too --- ansible-docker.sh | 62 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/ansible-docker.sh b/ansible-docker.sh index 067ce8c..3e94cd1 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -5,23 +5,17 @@ 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 """ + if [ ! -f ansible.cfg ]; then + echo -e """ [defaults] inventory = host.ini nocows = True @@ -34,15 +28,53 @@ stdout_callback = yaml ansible_python_interpreter=/usr/bin/python3 ansible_connection=local """ | tee -a ansible.cfg + fi # 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 -a 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 \ + --inventory localhost, \ + --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?}" + pushd ${GITHUB_WORKSPACE} + + # execute playbook + ansible-playbook \ + --connection=local \ + --inventory localhost, \ + --limit localhost ${TARGETS} +} + 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 From 3db80d792a125c5522b63119ff72521d6431c8e8 Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 10:46:17 +0100 Subject: [PATCH 3/8] put playbook into one line --- ansible-docker.sh | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ansible-docker.sh b/ansible-docker.sh index 3e94cd1..1a14859 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -48,10 +48,7 @@ ansible::test::role() { """ | tee -a deploy.yml # execute playbook - ansible-playbook \ - --connection=local \ - --inventory localhost, \ - --limit localhost deploy.yml + ansible-playbook --connection=local --inventory host.ini --limit localhost deploy.yml } ansible::test::playbook() { : "${TARGETS?No targets to check. Nothing to do.}" @@ -59,10 +56,7 @@ ansible::test::playbook() { pushd ${GITHUB_WORKSPACE} # execute playbook - ansible-playbook \ - --connection=local \ - --inventory localhost, \ - --limit localhost ${TARGETS} + ansible-playbook --connection=local --inventory host.ini --limit localhost ${TARGETS} } From f07bf33bc6e660fe15f20be0a1d07b1263dc6d42 Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 11:19:13 +0100 Subject: [PATCH 4/8] Do not allow custom ansible.cfg --- ansible-docker.sh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ansible-docker.sh b/ansible-docker.sh index 1a14859..4ba1a98 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -14,8 +14,7 @@ ansible::prepare() { pushd ${GITHUB_WORKSPACE} # generate ansible.cfg - if [ ! -f ansible.cfg ]; then - echo -e """ + echo -e """ [defaults] inventory = host.ini nocows = True @@ -27,11 +26,10 @@ fact_caching_timeout = 7200 stdout_callback = yaml ansible_python_interpreter=/usr/bin/python3 ansible_connection=local -""" | tee -a ansible.cfg - fi +""" | tee ansible.cfg # create host list - echo -e "[local]\nlocalhost ansible_python_interpreter=/usr/bin/python3 ansible_connection=local" | 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.}" From 6cf2d5fb3fc9d62149220f5a73e1477284b445bc Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 11:35:40 +0100 Subject: [PATCH 5/8] improve support for playbook execution --- action.yml | 17 +++++++++++++++++ ansible-docker.sh | 14 +++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index f43cbc6..1436eab 100644 --- a/action.yml +++ b/action.yml @@ -8,11 +8,28 @@ inputs: 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 runs: using: docker image: Dockerfile env: TARGETS: ${{ inputs.targets }} + HOSTS: ${{ inputs.hosts }} + GROUP: ${{ inputs.group }} branding: icon: 'aperture' color: 'green' diff --git a/ansible-docker.sh b/ansible-docker.sh index 4ba1a98..9c5f2e3 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -8,6 +8,7 @@ set -x # [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?}" @@ -46,27 +47,30 @@ ansible::test::role() { """ | tee -a deploy.yml # execute playbook - ansible-playbook --connection=local --inventory host.ini --limit 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 --limit localhost ${TARGETS} + ansible-playbook --connection=local --inventory host.ini ${TARGETS} } - if [ "$0" = "$BASH_SOURCE" ] ; then >&2 echo -E "\nRunning Ansible debian check...\n" ansible::prepare if [[ "${TARGETS}" == *.yml ]] then - echo -E "\nansible playbook detected\ninitialize playbook testing...\n" + echo -e "\nansible playbook detected\ninitialize playbook testing...\n" ansible::test::playbook else - echo -E "\nno playbook detected\ninitialize role testing...\n" + echo -e "\nno playbook detected\ninitialize role testing...\n" ansible::test::role fi fi From c8f21de4e74da4af06a18dfe56433320785eecb0 Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 18 Feb 2020 17:31:47 +0100 Subject: [PATCH 6/8] add missing submodule support --- ansible-docker.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ansible-docker.sh b/ansible-docker.sh index 9c5f2e3..df18328 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -62,6 +62,8 @@ ansible::test::playbook() { ansible-playbook --connection=local --inventory host.ini ${TARGETS} } +# make sure git is up to date +git submodule update --init --recursive if [ "$0" = "$BASH_SOURCE" ] ; then >&2 echo -E "\nRunning Ansible debian check...\n" ansible::prepare From 6cf3346189edcc1f690e20cfc144de332b48d0d1 Mon Sep 17 00:00:00 2001 From: L3D Date: Wed, 19 Feb 2020 12:21:36 +0100 Subject: [PATCH 7/8] Add support for ansible galaxy --- action.yml | 11 +++++++++++ ansible-docker.sh | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/action.yml b/action.yml index 1436eab..ae53d42 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,16 @@ inputs: 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 @@ -30,6 +40,7 @@ runs: 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 df18328..061ca3f 100755 --- a/ansible-docker.sh +++ b/ansible-docker.sh @@ -64,6 +64,12 @@ ansible::test::playbook() { # 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::prepare From 62017393215848f7e1034d4017d4a1004e56baad Mon Sep 17 00:00:00 2001 From: L3D Date: Thu, 20 Feb 2020 08:35:24 +0100 Subject: [PATCH 8/8] update explaination in README --- README.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 13f0f14..908a0f1 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,36 @@ jobs: uses: roles-ansible/check-ansible-debian-stable-action@master with: targets: "./" - # [required] - # Paths to your ansible role or playboox.yml you want to test - # Some Examples: - # targets: "role/my_role/" - # targets: "site.yml" + # [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: