|
--- |
|
- hosts: localhost |
|
|
|
tasks: |
|
- name: Update Ubuntu system |
|
become: yes |
|
apt: |
|
name: "*" |
|
state: latest |
|
update_cache: yes |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: WSL needed packages for SystemD |
|
become: yes |
|
apt: |
|
name: '{{ packages }}' |
|
vars: |
|
packages: |
|
- daemonize |
|
- dbus-user-session |
|
- fontconfig |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Fix SystemD if we are on WSL Part I |
|
become: yes |
|
copy: |
|
dest: "/usr/sbin/start-systemd-namespace" |
|
owner: root |
|
group: root |
|
mode: 644 |
|
force: no |
|
content: | |
|
#!/bin/bash |
|
|
|
SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target" |
|
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')" |
|
if [ "$LOGNAME" != "root" ] && ( [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ] ); then |
|
export | sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}' | \ |
|
grep -E -v "^(BASH|BASH_ENV|DIRSTACK|EUID|GROUPS|HOME|HOSTNAME|\ |
|
IFS|LANG|LOGNAME|MACHTYPE|MAIL|NAME|OLDPWD|OPTERR|\ |
|
OSTYPE|PATH|PIPESTATUS|POSIXLY_CORRECT|PPID|PS1|PS4|\ |
|
SHELL|SHELLOPTS|SHLVL|SYSTEMD_PID|UID|USER|_)(=|\$)" > "$HOME/.systemd-env" |
|
export PRE_NAMESPACE_PATH="$PATH" |
|
export PRE_NAMESPACE_PWD="$(pwd)" |
|
exec sudo /usr/sbin/enter-systemd-namespace "$BASH_EXECUTION_STRING" |
|
fi |
|
if [ -n "$PRE_NAMESPACE_PATH" ]; then |
|
export PATH="$PRE_NAMESPACE_PATH" |
|
unset PRE_NAMESPACE_PATH |
|
fi |
|
if [ -n "$PRE_NAMESPACE_PWD" ]; then |
|
cd "$PRE_NAMESPACE_PWD" |
|
unset PRE_NAMESPACE_PWD |
|
fi |
|
register: wsl_systemd_fix1 |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Fix SystemD if we are on WSL Part II |
|
become: yes |
|
copy: |
|
dest: "/usr/sbin/enter-systemd-namespace" |
|
owner: root |
|
group: root |
|
mode: 754 |
|
force: no |
|
content: | |
|
#!/bin/bash --norc |
|
|
|
if [ "$LOGNAME" != "root" ]; then |
|
echo "You need to run $0 through sudo" |
|
exit 1 |
|
fi |
|
|
|
if [ -x /usr/sbin/daemonize ]; then |
|
DAEMONIZE=/usr/sbin/daemonize |
|
elif [ -x /usr/bin/daemonize ]; then |
|
DAEMONIZE=/usr/bin/daemonize |
|
else |
|
echo "Cannot execute daemonize to start systemd." |
|
exit 1 |
|
fi |
|
|
|
if ! command -v /lib/systemd/systemd > /dev/null; then |
|
echo "Cannot execute /lib/systemd/systemd." |
|
exit 1 |
|
fi |
|
|
|
if ! command -v /usr/bin/unshare > /dev/null; then |
|
echo "Cannot execute /usr/bin/unshare." |
|
exit 1 |
|
fi |
|
|
|
SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target" |
|
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')" |
|
if [ -z "$SYSTEMD_PID" ]; then |
|
"$DAEMONIZE" /usr/bin/unshare --fork --pid --mount-proc bash -c 'export container=wsl; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; exec '"$SYSTEMD_EXE" |
|
while [ -z "$SYSTEMD_PID" ]; do |
|
echo "Sleeping for 1 second to let systemd settle" |
|
sleep 1 |
|
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')" |
|
done |
|
fi |
|
|
|
USER_HOME="$(getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}')" |
|
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then |
|
if [ -n "$1" ] && [ "$1" != "bash --login" ] && [ "$1" != "/bin/bash --login" ]; then |
|
exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a \ |
|
/usr/bin/sudo -H -u "$SUDO_USER" \ |
|
/bin/bash -c 'set -a; [ -f "$HOME/.systemd-env" ] && source "$HOME/.systemd-env"; set +a; exec bash -c '"$(printf "%q" "$@")" |
|
else |
|
exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a /bin/login -p -f "$SUDO_USER" |
|
fi |
|
echo "Existential crisis" |
|
exit 1 |
|
fi |
|
register: wsl_systemd_fix2 |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Fix SystemD if we are on WSL Part III |
|
become: yes |
|
copy: |
|
dest: "/etc/sudoers.d/00-wsl-systemd" |
|
owner: root |
|
group: root |
|
mode: 440 |
|
force: no |
|
content: | |
|
Defaults env_keep += WSLPATH |
|
Defaults env_keep += WSLENV |
|
Defaults env_keep += WSL_INTEROP |
|
Defaults env_keep += WSL_DISTRO_NAME |
|
Defaults env_keep += PRE_NAMESPACE_PATH |
|
Defaults env_keep += PRE_NAMESPACE_PWD |
|
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/enter-systemd-namespace |
|
validate: visudo -cf %s |
|
register: wsl_systemd_fix3 |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Fix SystemD if we are on WSL Part IV |
|
become: yes |
|
blockinfile: |
|
path: "/etc/bash.bashrc" |
|
marker: "# {mark} WSL2 SystemD workaround" |
|
insertafter: EOF |
|
block: | |
|
# Start or enter a PID namespace in WSL2 |
|
source /usr/sbin/start-systemd-namespace |
|
register: wsl_systemd_fix4 |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- pause: |
|
prompt: | |
|
We need to terminate the session for the WSL fix! |
|
Please reopen your WSL and rerun this script with |
|
|
|
ansible-playbook -K ~/ansible-microk8s.yml |
|
|
|
Press ENTER to continue... |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- (wsl_systemd_fix1 is changed) or |
|
(wsl_systemd_fix2 is changed) or |
|
(wsl_systemd_fix3 is changed) or |
|
(wsl_systemd_fix4 is changed) |
|
|
|
|
|
- name: We need to reboot WSL |
|
shell: "wsl.exe -t {{ ansible_facts['env']['WSL_DISTRO_NAME'] }}" |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- (wsl_systemd_fix1 is changed) or |
|
(wsl_systemd_fix2 is changed) or |
|
(wsl_systemd_fix3 is changed) or |
|
(wsl_systemd_fix4 is changed) |
|
|
|
- name: Install playbook deps |
|
become: yes |
|
apt: |
|
name: '{{ packages }}' |
|
vars: |
|
packages: |
|
- python3-apt |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Install 3rd party repo keys |
|
become: yes |
|
apt_key: |
|
url: '{{ item }}' |
|
state: present |
|
with_items: |
|
- https://deb.nodesource.com/gpgkey/nodesource.gpg.key |
|
- https://apt.releases.hashicorp.com/gpg |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Install 3rd party repos |
|
become: yes |
|
apt_repository: |
|
repo: '{{ item.repo }}' |
|
filename: '{{ item.filename }}' |
|
update_cache: yes |
|
loop: |
|
- { repo: 'deb https://deb.nodesource.com/node_14.x focal main', filename: 'nodesource' } |
|
- { repo: 'ppa:git-core/ppa', filename: ''} |
|
- { repo: 'deb [arch=amd64] https://apt.releases.hashicorp.com focal main', filename: 'hashicorp'} |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Install utils |
|
become: yes |
|
apt: |
|
name: '{{ packages }}' |
|
vars: |
|
packages: |
|
- git |
|
- git-extras |
|
- git-lfs |
|
- git-quick-stats |
|
- git-secrets |
|
- git-sizer |
|
- git-svn |
|
- gitbatch |
|
- mc |
|
- pigz |
|
- xz-utils |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Install utils for WSL |
|
become: yes |
|
apt: |
|
name: '{{ packages }}' |
|
vars: |
|
packages: |
|
- keychain |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Restart snapd.seeded.service |
|
become: yes |
|
systemd: |
|
name: snapd.seeded.service |
|
state: reloaded |
|
enabled: yes |
|
when: |
|
- ansible_facts['env']['WSLENV'] is defined |
|
- ansible_facts['env']['WSL_DISTRO_NAME'] is defined |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Install MicroK8s |
|
become: yes |
|
snap: |
|
name: microk8s |
|
channel: "1.19/stable" |
|
classic: yes |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Add current user "{{ ansible_user }}" to group microk8s |
|
become: yes |
|
user: |
|
name: "{{ ansible_user }}" |
|
groups: microk8s |
|
append: yes |
|
|
|
- name: Enable Mikrok8s Helm3 and Kubectl |
|
shell: sudo -iu {{ansible_user_id}} microk8s enable kubectl helm3 |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Add bash completion for microk8s.kubectl |
|
shell: sudo -iu {{ansible_user_id}} echo "source <(microk8s.kubectl completion bash)" >> ~/.bashrc |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |
|
|
|
- name: Add bash completion for microk8s.helm3 |
|
shell: sudo -iu {{ansible_user_id}} echo "source <(microk8s.helm3 completion bash)" >> ~/.bashrc |
|
when: |
|
- ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] >= '20' |