Repository navigation
Expand file tree
/
Copy pathbootstrap.sh
More file actions
159 lines (131 loc) · 5.24 KB
/
Copy pathbootstrap.sh
File metadata and controls
159 lines (131 loc) · 5.24 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# --- Configuration ---
ANSIBLE_REPO="https://github.com/brootware/ansible-machine.git"
REPO_DIR="/tmp/ansible-machine"
# --- Helper Functions ---
usage() {
cat <<EOF
Usage: $(basename "$0") [options]
Bootstrap script for ansible-machine configuration.
Options:
-m, --mac Run the setup for macOS only (tags: mac).
-d, --dotfiles Run the setup for dotfiles only (tags: onlydotfiles).
-h, --help Display this help message and exit.
If no options are provided, the script performs a full setup for a new
Debian/Ubuntu-based system.
EOF
exit 0
}
install_deps_debian() {
echo ">>> Installing dependencies for Debian/Ubuntu..."
sudo apt-get update
sudo apt-get install -y zsh curl git pipx python3-passlib
# echo ">>> Installing Oh My Zsh non-interactively..."
# if [ ! -d "$HOME/.oh-my-zsh" ]; then
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# else
# echo "Oh My Zsh is already installed. Skipping."
# fi
echo ">>> Installing Ansible with pipx..."
pipx install --include-deps ansible
pipx ensurepath
pipx inject ansible passlib
echo ">>> Setting root password (required for 'su' method BECOME password prompt)..."
echo "Please enter a new password for the root user."
sudo passwd root
}
install_galaxy_collections() {
echo ">>> Installing Ansible Galaxy collections..."
ansible-galaxy collection install ansible.posix community.general
}
verify_host_in_inventory() {
echo ">>> Cloning/updating repository to check for hostname..."
if [ -d "$REPO_DIR/.git" ]; then
(cd "$REPO_DIR" && git pull)
else
git clone "$ANSIBLE_REPO" "$REPO_DIR"
fi
echo ">>> Verifying hostname exists in inventory..."
if ! grep -q -E "^\s+($(hostname)|$(hostname -f)):" "$REPO_DIR/hosts.yml"; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >&2
echo "ERROR: Host '$(hostname)' not found in hosts.yml." >&2
echo "Please add this host to '$REPO_DIR/hosts.yml' and commit the change to your repository before proceeding." >&2
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >&2
exit 1
fi
echo ">>> Host '$(hostname)' found in inventory."
}
run_ansible_full() {
echo ">>> Running full Ansible playbook..."
verify_host_in_inventory
install_galaxy_collections
# read -rp "Enter target username: " target_username
# read -rsp "Enter password for: (default: ${target_username}): " user_passwd
# read -rp "Enter target group name (default: ${target_username}): " target_group
# read -rp "Enter target user home directory (default: /home/${target_username}): " target_user_home
# Use defaults for group and home if not provided
target_group=${target_group:-$target_username}
target_user_home=${target_user_home:-/home/$target_username}
echo ">>> Running ansible-pull..."
# The -U flag handles both cloning for the first time and updating on subsequent runs.
ansible-pull -U "$ANSIBLE_REPO" --purge -K -e "user_passwd=$(read -sp 'Enter password for brootware user: ' p && echo "$p")"
# ansible-pull -U "$ANSIBLE_REPO" --purge -K \
# -e "target_username=${target_username}" \
# -e "user_passwd=${user_passwd}" \
# -e "target_group=${target_group}" \
# -e "target_user_home=${target_user_home}"
}
run_ansible_mac() {
echo ">>> Running Ansible for macOS setup..."
install_galaxy_collections
verify_host_in_inventory
# The -K flag will prompt for the sudo password.
ansible-pull -U "$ANSIBLE_REPO" -K --tags "mac" -vv
}
run_ansible_dotfiles() {
install_galaxy_collections
verify_host_in_inventory
echo ">>> Running Ansible for dotfiles setup..."
echo ">>> Setting root password (required for 'su' method BECOME password prompt)..."
echo "Please enter a new password for the root user."
sudo passwd root
read -rp "Enter target username: " target_username
read -rp "Enter target group name (default: ${target_username}): " target_group
read -rp "Enter target user home directory (default: /home/${target_username}): " target_user_home
# Use defaults for group and home if not provided
target_group=${target_group:-$target_username}
target_user_home=${target_user_home:-/home/$target_username}
ansible-pull -U "$ANSIBLE_REPO" -K --tags "onlydotfiles" \
-e "target_username=${target_username}" \
-e "target_group=${target_group}" \
-e "target_user_home=${target_user_home}"
}
# --- Main Logic ---
if [ "$#" -gt 0 ]; then
while [ "$#" -gt 0 ]; do
case "$1" in
-m|--mac)
run_ansible_mac
shift
;;
-d|--dotfiles)
run_ansible_dotfiles
shift
;;
-h|--help)
usage
;;
*)
echo "Error: Invalid option '$1'"
usage
;;
esac
done
else
# Default action: full setup for Debian/Ubuntu
install_deps_debian
run_ansible_full
fi
echo "✅ Bootstrap complete!"