-
Notifications
You must be signed in to change notification settings - Fork 0
/
nix_anisble.sh
129 lines (111 loc) · 3.11 KB
/
nix_anisble.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
set -e
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to detect the operating system
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$NAME
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS=$DISTRIB_ID
elif [ -f /etc/debian_version ]; then
OS=Debian
else
OS=$(uname -s)
fi
}
# Install Nix
install_nix() {
echo "Installing Nix..."
if command_exists nix; then
echo "Nix is already installed."
else
sh <(curl -L https://nixos.org/nix/install) --daemon
# Source nix
if [ -e ~/.nix-profile/etc/profile.d/nix.sh ]; then
. ~/.nix-profile/etc/profile.d/nix.sh
elif [ -e /etc/profile.d/nix.sh ]; then
. /etc/profile.d/nix.sh
fi
# Add Nix to shell configuration
if [ -f ~/.bashrc ]; then
echo '. ~/.nix-profile/etc/profile.d/nix.sh' >> ~/.bashrc
fi
if [ -f ~/.bash_profile ]; then
echo '. ~/.nix-profile/etc/profile.d/nix.sh' >> ~/.bash_profile
fi
if [ -f ~/.zshrc ]; then
echo '. ~/.nix-profile/etc/profile.d/nix.sh' >> ~/.zshrc
fi
fi
}
# Install Ansible using Nix
install_ansible_with_nix() {
echo "Installing Ansible using Nix..."
nix-env -iA nixpkgs.ansible
nix-env -iA nixpkgs.python3Packages.pip
}
# Install additional Ansible requirements
install_ansible_requirements() {
echo "Installing Ansible requirements..."
pip3 install --user ansible-core
ansible-galaxy collection install community.general
}
# Set up Ansible configuration
setup_ansible_config() {
echo "Setting up Ansible configuration..."
mkdir -p ~/.ansible
cat <<EOF > ~/.ansible/ansible.cfg
[defaults]
inventory = ~/.ansible/hosts
remote_user = $(whoami)
host_key_checking = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
EOF
echo "Creating a sample inventory file..."
cat <<EOF > ~/.ansible/hosts
[local]
localhost ansible_connection=local
[servers]
# Add your server IP addresses or hostnames here
# example_server ansible_host=192.168.1.10
EOF
}
# Main execution
main() {
detect_os
echo "Detected OS: $OS"
install_nix
install_ansible_with_nix
install_ansible_requirements
setup_ansible_config
# Verify installations
if command_exists nix; then
echo "Nix has been successfully installed."
nix --version
else
echo "Nix installation failed."
exit 1
fi
if command_exists ansible; then
echo "Ansible has been successfully installed."
ansible --version
else
echo "Ansible installation failed."
exit 1
fi
echo "Installation complete. Please log out and log back in, or source your shell configuration file to use Nix and Ansible."
echo "Don't forget to edit ~/.ansible/hosts to add your target servers."
}
# Run the main function
main