forked from gcoop-libre/ansible_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awx-csv-host2all
executable file
·143 lines (97 loc) · 3.14 KB
/
awx-csv-host2all
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2021 Osiris Alejandro Gomez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# shellcheck disable=SC1090
# shellcheck disable=SC1091
# shellcheck source=/dev/null
DIR_BIN="$(dirname "$0")" && source "$DIR_BIN/awx-common"
function usage()
{
cat << EOF
Usage:
\`\`\`bash
$BIN CSV [FILTER_NAME] [FILTER_GROUP]
\`\`\`
Read CSV and generate files:
- \`config\` (all hosts order by group and host)
Host wst12345 wst01dev
Hostname wst01dev.example.com
Host wst12346 wst01prd
Hostname wst01prd.example.com
- \`groups\` (all groups)
dev
prd
- \`host_vars/INVENTORY_HOST.yml\` (one file per host)
---
ansible_host: 10.0.0.45
primary_maccaddress: ca:fe:ca:fe:01:23
hostname: wst01dev
inventory_serial: W31415926
- \`hosts\` (all hosts in groups)
[dev]
wst12345
[prd]
wst12346
\`\`\`
Example:
\`\`\`bash
$BIN wst.csv wst dev
\`\`\`
EOF
exit 0
}
[[ "$1" =~ ^[-]+(h|help) ]] && usage
FILTER_NAME='.*'
FILTER_GROUP='.*'
[[ -z "$1" ]] && die "EMPTY CSV"
[[ -e "$1" ]] || die "NOT FOUND $1"
CSV="$1"
[[ -z "$2" ]] || FILTER_NAME="$2"
[[ -z "$3" ]] || FILTER_GROUP="$3"
[[ -z "$DEFAULT_DOMAIN" ]] || DOMAIN="$DEFAULT_DOMAIN"
[[ -z "$DEFAULT_SEP" ]] || SEP="$DEFAULT_SEP"
TMP="$(mktemp)"
awk "/$FILTER_NAME/ {print \$0}" "$CSV" \
| tr -d "'" > "$TMP"
awk -F "$SEP" '{print $2}' "$TMP" \
| grep -v '^$' \
| grep -Eo "$FILTER_GROUP" \
| sort -u > 'groups'
true > hosts
true > config
while read -r GROUP
do
printf "\\n\\n[%s]\\n" "$GROUP" >> hosts
grep "$GROUP" "$TMP" \
| tr -d "'" \
| while IFS=',' read -r HOST NAME IP MAC SERIAL
do
FQDN="$NAME.$DOMAIN"
YAML="host_vars/$HOST.yml"
mkdir -p host_vars
printf "%s\\n" "$HOST" >> hosts
printf "%s" "$(yaml_host_vars "$IP" "$MAC" "$NAME" "$SERIAL")" > "$YAML"
printf "%s" "$(ssh_config "$HOST" "$NAME" "$FQDN")" >> config
done
done < 'groups'
TOTAL_GROUPS="$(wc -l groups | awk '{print $1}')"
TOTAL_CONFIG="$(grep -c '^Host ' config)"
TOTAL_HOSTS="$(grep -v '^$' 'hosts' | grep -v '\[' | sort -u | wc -l)"
TOTAL_YAML="$(find host_vars -type f -iname '*.yml' | wc -l)"
printf "%-5s %s\\n" "$TOTAL_GROUPS" 'groups'
printf "%-5s %s\\n" "$TOTAL_CONFIG" 'hosts in config file'
printf "%-5s %s\\n" "$TOTAL_HOSTS" 'hosts'
printf "%-5s %s\\n" "$TOTAL_YAML" 'host_vars/*.yml files'