Skip to content

Instantly share code, notes, and snippets.

@konstruktoid
Last active April 26, 2022 11:52
Show Gist options
  • Save konstruktoid/576846137c523c9a34bd1a65e21f85fa to your computer and use it in GitHub Desktop.
Save konstruktoid/576846137c523c9a34bd1a65e21f85fa to your computer and use it in GitHub Desktop.
Script to calculate Ansible forks based on the number of CPUs.
#!/bin/bash -eux
if nproc &>/dev/null; then
CPUS=$(nproc)
elif grep -qc processor /proc/cpuinfo; then
CPUS=$(grep -c processor /proc/cpuinfo)
elif sysctl -n hw.ncpu &>/dev/null; then
CPUS=$(sysctl -n hw.ncpu)
else
CPUS=1
fi
FORKS=$(( CPUS * 4 ))
if [[ "${FORKS}" -lt 5 ]]; then
FORKS=5
fi
export ANSIBLE_FORKS="${FORKS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment