-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-docker.sh
executable file
·92 lines (81 loc) · 2.66 KB
/
ansible-docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env bash
set -Eeuo pipefail
set -x
# Generates client.
# env:
# [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 ansible.cfg
cat <<EOF | tee ansible.cfg
[defaults]
inventory = hosts.ini
nocows = true
host_key_checking = false
forks = 20
fact_caching = jsonfile
fact_caching_connection = $HOME/facts
fact_caching_timeout = 7200
ansible_python_interpreter=/usr/bin/python3
ansible_connection=local
EOF
# create host list
cat <<EOF | tee hosts.ini
[local]
localhost ansible_python_interpreter=/usr/bin/python3 ansible_connection=local
EOF
}
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
cat <<EOF | tee -a deploy.yml
---
- name: test a ansible role
hosts: localhost
tags: default
roles:
- "${TARGETS}"
EOF
# execute playbook
ansible-playbook --connection=local --limit localhost deploy.yml --tags "${TAGS}" --skip-tags "${SKIPTAGS}"
}
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}"
cat <<EOF | tee hosts.ini
[${GROUP}]
${HOSTS} ansible_python_interpreter=/usr/bin/python3 ansible_connection=local ansible_host=127.0.0.1
EOF
# execute playbook
# shellcheck disable=SC2086
ansible-playbook --connection=local --inventory hosts.ini ${TARGETS}
}
# make sure git is up to date
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
git submodule update --init --recursive
if [[ "${REQUIREMENTS}" == *.yml ]]
then
ansible-galaxy install -r "${REQUIREMENTS}"
else
[ -n "${REQUIREMENTS}" ] && ansible-galaxy install "${REQUIREMENTS}"
fi
if [ "$0" = "${BASH_SOURCE[*]}" ] ; then
>&2 printf "Running Ansible centos check...\n"
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