forked from boochtek/mac_config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_for_mac_serial_number.sh
executable file
·80 lines (57 loc) · 1.98 KB
/
inventory_for_mac_serial_number.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
#!/bin/bash -e
# Returns the hostname and groups for the local (Mac) system, based on hardware serial number.
# Used as a dynamic inventory script for Ansible.
# See http://docs.ansible.com/ansible/developing_inventory.html for details.
# TODO:
# * Return an empty hash if given --host
# * Error out on anything other than --host or --list or --all
# * Move configuration to an external file.
# Mappings of serial numbers to hostname and groups.
# You can find your serial number in 'About This Mac'.
# Or you can run `ioreg -l | grep IOPlatformSerialNumber` from the command line.
# The groups correspond to roles (subdirectories in the `roles` directory).
HOST_MAPPINGS="
C02JG2RXDKQ4 hope mac workstation dev
C02TH22UGTDY FLD-ML-00021621 mac workstation dev f5
C02XX2Z3JG5M WM00219-ENG-CBuchek mac workstation dev weedmaps
C02ZX059MD6R the-good-place mac workstation dev
C02C15H3MD6M loaner mac workstation dev truelink
X6T04FX6GD lasso mac workstation dev debtbook
"
function main() {
local hostname="$(hostname)"
local groups="$(groups_for_host $hostname)"
echo "$(list_json $hostname "$groups")"
}
function hostname() {
local hostname=""
hostname="$(hostname_for_serial_number)"
[[ ! -z "$hostname" ]] && echo $hostname && return 0
return 1
}
function groups_for_host() {
local hostname=$1
grep $hostname <<<"$HOST_MAPPINGS" | awk '{$1 = ""; $2 = ""; print $0}'
}
function list_json() {
local hostname=$1
local groups="$2"
local first_group=0
echo "{"
for group in $groups ; do
if [[ $first_group -eq 0 ]]; then
first_group=1
else
echo ","
fi
echo " \"$group\": [ \"$hostname\" ]"
done
echo "}"
}
function serial_number() {
ioreg -c IOPlatformExpertDevice -d 2 | awk '/IOPlatformSerialNumber/ { print $3; }' | tr -d '"'
}
function hostname_for_serial_number() {
grep "^$(serial_number)" <<<"$HOST_MAPPINGS" | awk '{print $2}'
}
main