Commit b41587d3 authored by Torsten Nielsen's avatar Torsten Nielsen
Browse files

Initial commit

# How to install and set up Ansible on (Ubuntu) Linux.
## Different ways to install Ansible
Ansible is based on Python (not Perl), but still regarding install, "There's more than one way to do it" [TMTOWTDI](
There are a number of ways to install Ansible (on Ubuntu):
- System wide install using (deb) packages (with or without PPA): [Install Ansible on Ubuntu using apt](
## System wide installs (all users) using deb-packages
Use std. package from Ubuntu (installs version 2.9.6 on focal/20.04 at time of writing):
$ sudo apt update
$ sudo apt install ansible
Use PPA for slightly newer version of Ansible (2.10.12 for ansible-base on focal at time of writing):
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible-base
Since Ansible version 2.10 a lot of the default install has changed to a more modular install. Where Ansible used to include everything (and the kitchen sink), it now has a lean base install, which is again changed to a 'core' install from version 4.x.x and up. Besides the base (or core) install there is community and vendor collections, which have their own versioning.
Se more here: [Ansible 3.0.0 blog post]( and [ blog post]( or [Ansible 3 Release Notes]( or [Ansible 4 Release Notes](
# How to set up and use Python virtual environments for Ansible
> Python's venv module enables different Ansible versions to coexist on the same machine. New features can be tested while keeping older versions intact and working.
Python provides different ways to create virtual environment. In this guide Python virtual environments provided by the `venv` module is used. An more featureful alternative could be [virtualenv]( as used in the [Ansible doc](
Why use Python virtual environments for Ansible development?
Virtual environments are like a virtual machine or Linux `chroot`, but they create an isolated structure of lightweight directories separated from actual Python system directories. That means you can have different sets of Python environments, each with different versions of modules, files, or configurations.
Ansible is based on Python and leverages many Python modules and plugins. When you test something new, it could require you to update a module that your Ansible installation depends on. Upgrading a component that your production environment depends on defeats the purpose of testing. Still, you can have different versions of Ansible and other important Python modules in a dedicated test directory with a virtual environment.
Get set up
Verify the installed Python version and path:
$ sudo apt install python3-pip python3-venv
$ python3 -V
Python 3.8.10
$ which python3
Create a directory structure for the virtual environments:
$ mkdir -p ~/ansible/python-venvs
$ cd !$
Create a new Python virtual environment
Create a virtual environment using the `python3 -m venv <environment-name>` command. You can give any name to your Python virtual environment. I want to try the `Ansible 2.9` version, so I named it in a way to identify the directory easily:
$ python3 -m venv ansible2.9.6
$ ls
Activate a Python virtual environment
After creating a virtual environment, you must enter the environment manually. This changes your active environment variables from your current shell to those required for Python to create a virtual environment:
$ source ansible2.9.6/bin/activate
(ansible2.9.6)$ python3 -V
Python 3.6.8
Next, upgrade the preferred installer program (`pip`) inside your virtual environment:
(ansible2.9.6)$ python3 -m pip install --upgrade pip
Install Ansible in a virtual environment
With your virtual environment set up and active, you can install a dedicated version of Ansible into it. This example installs version 2.9.6, but you can install any version that's current (or in development):
(ansible2.9.6)$ python3 -m pip install ansible==2.9.6
(ansible2.9.6)$ which ansible
Verify your new installation:
(ansible2.9.6)$ ansible --version
ansible 2.9.6
config file = None
configured module search path = ['/home/auXXX/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/auXXX/ansible/python-venvs/ansible2.9.6/lib/python3.6/site-packages/ansible
executable location = /home/auXXX/ansible/python-venvs/ansible2.9.6/bin/ansible
python version = 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0]
Ansible was not able to locate a config file - we'll fix that shortly. The Ansible executable and python modules are located in the virtual environment, but the Ansible module search path is outside.
Anything installed using pip is going in the venv folder for the active virtual environment.
Just like Ansible (base/core) can be installed in different ways. The Ansible configuration define, where things like inventory, roles and collections should go.
Ansible Configuration
While running an Ansible command, the command looks for its configuration file in a predefined order, as follows:
ANSIBLE_CFG: This environment variable is used, provided it is set
ansible.cfg: This is located in the current directory
~/.ansible.cfg: This is located in the user's home directory
I'll describe an example slightly different than the examples given in [Ansible User Guide](
If we use '~/ansible' as "current" or "working" directory, when we interact with Ansible, it makes sence to put 'ansible.cfg' here. This way we could have different subfolders with diffent setups.
$ cd ~/ansible
$ ls -F
ls -F
ansible.cfg collections/ inventory/ myroles/ playbooks/ python-venvs/ roles/
Example configuration:
$ cat ansible.cfg
# Paths to search for collections and roles, colon separated
collections_paths = ~/ansible/collections
roles_path = roles:myroles # put your own roles in separate folder
# create hosts files under inventory
inventory = inventory
gather_timeout = 10
private_key_file = ~/.ssh/ansible_key # generate and distribute key with ssh
host_key_checking = False # Mainly for convenience enable for more secure setup
retry_files_enabled = False # Enable if want this
# Default Ansible remote user - if nothing is set in playbook
remote_user = ansible # user with sudo rights on managed system
# For more example settings
# or use
# ansible-config list
Install Ansible roles or collections
An example (community) collection to install could be [Ansible Community Docker]( using the method in [Ansible doc Collection](
You can also install Ansible roles and collections into a local path using '-p'.
(ansible2.9.6)$ ansible-galaxy collection install community.docker -p collections
Process install dependency map
Starting collection install process
Installing 'community.docker:1.9.1' to '/home/auXXX/ansible/collections/ansible_collections/community/docker'
Collections and Roles can be used across different versions of Ansible, but if you need special versions or want to isolate some, that don't work with certain versions of Ansible, then it would make sence to isolate them in their own folders.
Deactivate a Python virtual environment
Once you're finished working inside your Python virtual environment (or it's time to switch to another Python virtual environment), you can deactivate it:
(ansible2.9)$ deactivate
Create another Python virtual environment for Ansible 3.0
After deactivating the first virtual environment, try creating another Python virtual environment to understand the power this technology grants you. In this new environment, you can install Ansible 3.4.0 [or any other version](
$ cd ~/ansible
$ python3 -m venv python-venvs/ansible3.4.0
$ . python-venvs/ansible3.4.0/bin/activate
(ansible3.4.0)$ python3 -m pip install --upgrade pip
(ansible3.4.0)$ python3 -m pip install wheel
(ansible3.4.0)$ python3 -m pip install ansible==3.4.0
Wrap up
Python virtual environments give you the freedom to test whatever version of modules, plugins, and Python packages you need. That includes vital sysadmin software like Ansible, so you can test new Ansible features without disturbing your system install.
Modified from: [Source](
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment