Windows install Ansible via msys2

借助 msys2 在windows上安装 ansible

安装完成msys2后,复制脚本内容即可直接安装ansible。
下面是bash脚本。

#!/usr/bin/bash

files=("mirrorlist.mingw32" "mirrorlist.mingw64" "mirrorlist.msys")
mirrors=("Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686" "Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64" "Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/\$arch")
path="/etc/pacman.d/"
cfg="~/.ansible.cfg"

for i in {0..2}
do
if grep --quiet "^${mirrors[$i]}" ${files[$i]}; then
continue
fi
sed -i "s/^Server /## &/g" $path${files[$i]}
sed -i "$ a${mirrors[$i]}" $path${files[$i]}
echo "Add new mirror ${mirrors[$i]} to ${files[$i]}"
done

pacman -Sy
pacman -S python3 python3-pip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# https://github.com/geerlingguy/JJG-Ansible-Windows/issues/6
fix_ssh_connection() {
if grep --quiet "^[ssh_connection]" $cfg && grep --quiet "^ssh_args = -o ControlMaster=no" $cfg; then
# a simple check; maybe you should add this option manually
return
fi
echo "[ssh_connection]" >> $cfg
echo "ssh_args = -o ControlMaster=no" >> $cfg
}

# https://gist.github.com/DaveB93/db94a6b310e08c928c0778f766562ab0
# author: DaveB93
which ansible >/dev/null 2>&1
if [ $? -ne 0 ];
then
echo "Installing Ansible..."
sleep 5
pushd .
cd ~
pacman -S libyaml-devel mingw-w64-x86_64-libsodium libffi libffi-devel gcc pkg-config make openssl-devel openssh libcrypt-devel sshpass --noconfirm --needed
SODIUM_INSTALL=system CFLAGS=`pkg-config --cflags libffi` LDFLAGS=`pkg-config --libs libffi` python3 -m pip install cffi --no-binary :all:
SODIUM_INSTALL=system CFLAGS=`pkg-config --cflags libffi` LDFLAGS=`pkg-config --libs libffi` C_INCLUDE_PATH=/mingw64/include LIBRARY_PATH=/mingw64/lib python3 -m pip install pynacl
python3 -m pip install ansible --no-binary :all:
popd
fi
which ansible >/dev/null 2>&1
if [ $? -eq 0 ];
then
echo "Ansible installed."
fix_ssh_connection
else
echo "Ansible not installed."
fi