Skip to content

Instantly share code, notes, and snippets.

@zekefast
Created July 15, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekefast/d93db245ab3d050aaf3f6fb15742b81c to your computer and use it in GitHub Desktop.
Save zekefast/d93db245ab3d050aaf3f6fb15742b81c to your computer and use it in GitHub Desktop.
Install Ansible on Debian system as described in Ansible's documentation https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-debian . Version of Ansible installed using this method usually fresher than the one may find in official's Debian's repositories.
#!/usr/bin/env bash
##
# Ansible installation script.
#
# See for more details: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-debian
#
# TODO:
# Stop immediately if any external program returns a non-zero exit status.
# Short: set -e
set -o errexit
# Treat unset variables as an error when substituting.
# Short: set -u
set -o nounset
# If set, the return value of a pipeline is the value of the last (rightmost)
# command to exit with a non-zero status, or zero if all commands in the
# pipeline exit successfully. This option is disabled by default.
set -o pipefail
# Uncomment for debugging
#
# Disable file name generation using metacharacters (globbing).
# Short: set -f
#set -o noglob
# Prints shell input lines as they are read.
# Short: set -v
#set -o verbose
# Print command traces before executing command.
# Short: set -x
#set -o xtrace
readonly __DIR__="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_URL="http://ppa.launchpad.net/ansible/ansible/ubuntu"
readonly ANSIBLE_PACKAGE_REPOSITORY_URL="${ANSIBLE_PACKAGE_REPOSITORY_URL:-$DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_URL}"
readonly DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_CODENAME="trusty"
readonly ANSIBLE_PACKAGE_REPOSITORY_CODENAME="${ANSIBLE_PACKAGE_REPOSITORY_CODENAME:-$DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_CODENAME}"
readonly DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_COMPONENT="main"
readonly ANSIBLE_PACKAGE_REPOSITORY_COMPONENT="${ANSIBLE_PACKAGE_REPOSITORY_COMPONENT:-$DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_COMPONENT}"
readonly DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_STRING="deb ${ANSIBLE_PACKAGE_REPOSITORY_URL} ${ANSIBLE_PACKAGE_REPOSITORY_CODENAME} ${ANSIBLE_PACKAGE_REPOSITORY_COMPONENT}"
readonly ANSIBLE_REPOSITORY_SOURCES_LIST_STRING="${ANSIBLE_REPOSITORY_SOURCES_LIST_STRING:-$DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_STRING}"
readonly DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH="/etc/apt/sources.list.d/ansible.list"
readonly ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH="${ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH:-$DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH}"
readonly DEFAULT_APT_KEY_SERVER_DOMAIN="keyserver.ubuntu.com"
readonly APT_KEY_SERVER_DOMAIN="${APT_KEY_SERVER_DOMAIN:-$DEFAULT_APT_KEY_SERVER_DOMAIN}"
readonly DEFAULT_APT_KEY_FINGERPRINT="93C4A3FD7BB9C367"
readonly APT_KEY_FINGERPRINT="${APT_KEY_FINGERPRINT:-$DEFAULT_APT_KEY_FINGERPRINT}"
readonly DEFAULT_ANSIBLE_PACKAGE_NAME="ansible"
readonly ANSIBLE_PACKAGE_NAME="${ANSIBLE_PACKAGE_NAME:-$DEFAULT_ANSIBLE_PACKAGE_NAME}"
main() {
while getopts 'h' _FLAG; do
case $_FLAG in
h)
usage
exit 0
;;
*)
usage
exit 1
esac
done
install_ansible
exit 0
}
usage() {
cat <<-EOS
Ansible installation script.
You have run with ROOT priviledges (using sudo, for example) as all the
commands for manipulation with package repositories require those priviledges.
Usage: $0 [OPTIONS...]
-h Displays this help.
Environment variables:
ANSIBLE_PACKAGE_REPOSITORY_URL (Default: $DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_URL)
Supported by APT (usually HTTP or HTTPS) url address of DEB packages repository.
ANSIBLE_PACKAGE_REPOSITORY_CODENAME (Default: $DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_CODENAME)
Repository's distribution codename is used to fetch packages.
It is possible it does not correspond your distribution codename, for example,
in case you are using Debian system, but Ansible documentation suggests to
install package from Ubuntu's PPA.
ANSIBLE_PACKAGE_REPOSITORY_COMPONENT (Default: $DEFAULT_ANSIBLE_PACKAGE_REPOSITORY_COMPONENT)
Component name of package repository which is used to fetch Ansible's packages,
usually used values are main, contrib or non-free.
ANSIBLE_REPOSITORY_SOURCES_LIST_STRING (Default: $DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_STRING)
String which will be inserted into package repository file, usually in
/etc/sources.list.d dictory.
ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH (Default: $DEFAULT_ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH)
Path to package repository source file in which
ANSIBLE_REPOSITORY_SOURCES_LIST_STRING string will be put.
APT_KEY_SERVER_DOMAIN (Default: $DEFAULT_APT_KEY_SERVER_DOMAIN)
Domain address of APT key server. It used to fetch repository's key
(the key which is used to sign packages in the repository).
APT_KEY_FINGERPRINT (Default: $DEFAULT_APT_KEY_FINGERPRINT)
Unique fingerprint of the key which is used by apt-key command to fetch
it from the server specified by APT_KEY_SERVER_DOMAIN.
ANSIBLE_PACKAGE_NAME (Default: $DEFAULT_ANSIBLE_PACKAGE_NAME)
Name of the Ansible package which will be used for installation.
Examples:
# Output help information about command usage and available options
$0 -h
EOS
}
install_ansible() {
add_apt_key
add_apt_repository
apt-get update
apt-get install "${ANSIBLE_PACKAGE_NAME}"
}
add_apt_key() {
apt-key adv --keyserver "${APT_KEY_SERVER_DOMAIN}" --recv-keys "${APT_KEY_FINGERPRINT}"
}
add_apt_repository() {
# GOTCHA:
# From https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#latest-releases-via-apt-debian
#
# NOTE:
# This method has been verified with the Trusty sources in Debian Jessie
# and Stretch but may not be supported in earlier versions.
echo "${ANSIBLE_REPOSITORY_SOURCES_LIST_STRING}" > "${ANSIBLE_REPOSITORY_SOURCES_LIST_FILE_PATH}"
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment