-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-ansible-playbooks.sh
executable file
·108 lines (90 loc) · 2.26 KB
/
create-ansible-playbooks.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# Taken from https://gist.github.com/Hobadee/37f215dc0621b35830331c82fd0d4279
# Script to create ansible playbook directories
# With thanks to https://gist.github.com/skamithi/11200462 for giving me the idea
# We attempt to lay everything out according to Ansible best practices:
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#playbooks-best-practices#directory-layout
gitinit(){
# Start with sensible .gitignore defaults
curl "https://www.gitignore.io/api/osx,linux,windows,ansible" > .gitignore
git init
}
usage(){
cat <<EOF
Usage:
$0 -p <Name> [-r <Role1>] [-r <Role2>] [-g <Ansible-GalaxyRole1>] [-g <AnsibleGalaxyRole2>]
-p Playbook name
-r Define a role. (Can be used multiple times)
-g Define a role to import from Ansible Galaxy. (Can be used multiple times)
-f Force creation of playbook, even if it already exists
-G DO NOT create a GIT repository for the new playbook
EOF
}
HELP=false
PLAYBOOK=false
NEWROLES=(common)
GALAXYROLES=()
FORCE=false
GIT=true
# File/Dir names
GIT_README=README.md
GIT_LICENSE=LICENSE.md
PLAYBOOK_MASTER=site.yml
PLAYBOOK_HOSTS=hosts
ANSIBLE_DIRS=(global_vars group_vars host_vars library module_utils filter_plugins roles)
while getopts :hp:r:g:fG OPTION
do
case $OPTION in
h)HELP=true
;;
p)PLAYBOOK=$OPTARG
;;
r)NEWROLES=($NEWROLES $OPTARG)
;;
g)GALAXYROLES=($GALAXYROLES $OPTARG)
;;
f)FORCE=true
;;
G)GIT=false
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ $HELP == true ]]; then
usage;
exit 0;
fi
if [[ $PLAYBOOK == false ]]; then
usage;
exit 1;
fi
if [[ -d $PLAYBOOK && $FORCE != true ]]; then
echo "Playbook already exists!"
exit 1;
fi
# create playbook
mkdir -p $PLAYBOOK
cd $PLAYBOOK
for i in ${ANSIBLE_DIRS[@]}; do
mkdir -p ${i}
done
touch $GIT_README $GIT_LICENSE
touch $PLAYBOOK_MASTER $PLAYBOOK_HOSTS
if [[ $GIT == true ]]; then
gitinit
fi
for i in ${NEWROLES[@]}; do
ansible-galaxy init ${i} --init-path=roles
done
for i in ${GALAXYROLES[@]}; do
ansible-galaxy install ${i} -p roles
done
exit 0