Commit 266ca24c authored by Torsten Nielsen's avatar Torsten Nielsen
Browse files

Fixed minor errors and added stuff about PPAs

parent b41587d3
......@@ -15,27 +15,25 @@ Use std. package from Ubuntu (installs version 2.9.6 on focal/20.04 at time of w
$ 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):
[Ansible docs' install guide for Ubuntu](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-ubuntu) also suggests the use of a [PPA](https://launchpad.net/~ansible/+archive/ubuntu/ansible), but [this PPA](https://launchpad.net/~ansible/+archive/ubuntu/ansible) does not support Ubuntu 20.04 aka "Focal", so if you are using a newer Ubuntu version, take at look at the [other PPAs](https://launchpad.net/~ansible) and maybe choose [ansible-3](https://launchpad.net/~ansible/+archive/ubuntu/ansible-3):
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible-base
$ sudo add-apt-repository --yes --update ppa:ansible/ansible-3
$ sudo apt install ansible
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.
Since Ansible version 2.10 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 has again changed to a 'core' install from version 4.x.x and up. Besides the base (or core) install, there are community and vendor collections, with their own versioning.
Se more here: [Ansible 3.0.0 blog post](https://blog.while-true-do.io/ansible-release-3-0-0/) and [Ansible.com blog post](https://www.ansible.com/blog/ansible-3.0.0-qa) or [Ansible 3 Release Notes](https://github.com/ansible-community/ansible-build-data/blob/main/3/CHANGELOG-v3.rst) or [Ansible 4 Release Notes](https://github.com/ansible-community/ansible-build-data/blob/c4ab2b13b7dca1ac14c49e5f56e5185fba500d87/4/CHANGELOG-v4.rst)
# 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](https://virtualenv.pypa.io/en/stable/) as used in the [Ansible doc](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-in-a-virtual-environment-with-pip).
> 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](https://virtualenv.pypa.io/en/stable/) as demonstrated in the [Ansible doc](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-in-a-virtual-environment-with-pip). Maybe take a look at [Pyenv](https://github.com/pyenv/pyenv).
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.
Virtual environments 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. If Ansible is installed using pip, then it can be a hassle to upgrade and older version has to be uninstalled first.
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.
......@@ -88,7 +86,7 @@ With your virtual environment set up and active, you can install a dedicated ver
(ansible2.9.6)$ python3 -m pip install ansible==2.9.6
(ansible2.9.6)$ which ansible
~/python-venv/ansible2.9/bin/ansible
~/python-venv/ansible2.9.6/bin/ansible
Verify your new installation:
......@@ -106,7 +104,7 @@ Ansible was not able to locate a config file - we'll fix that shortly. The Ansib
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.
Just like Ansible can be installed in different ways. The Ansible configuration define, where things like inventory, roles and collections should go.
Ansible Configuration
---------------------
......@@ -118,14 +116,13 @@ While running an Ansible command, the command looks for its configuration file i
~/.ansible.cfg: This is located in the user's home directory
/etc/ansible/ansible.cfg
I'll describe an example slightly different than the examples given in [Ansible User Guide](https://docs.ansible.com/ansible/latest/user_guide/sample_setup.html)
An example slightly different from the examples given in [Ansible User Guide](https://docs.ansible.com/ansible/latest/user_guide/sample_setup.html)
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.
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/
$ ls -F
ansible.cfg collections/ inventories/ myroles/ playbooks/ python-venvs/ roles/ host_vars/ group_vars/
Example configuration:
----------------------
......@@ -138,17 +135,18 @@ Example configuration:
roles_path = roles:myroles # put your own roles in separate folder
# create hosts files under inventory
inventory = inventory
inventory = inventories
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
retry_files_enabled = False # Enable if you want this
# Default Ansible remote user - if nothing is set in playbook
remote_user = ansible # user with sudo rights on managed system
remote_user = ansible # user with sudo privileges on managed system
[privilege_escalation]
#Change the defaults if needed and/or overwrite in playbook/CLI
#become=True
#become_method=sudo
#become_user=root
......@@ -163,10 +161,13 @@ Example configuration:
Install Ansible roles or collections
------------------------------------
Modules, Roles and Collections can be found at [Ansible Galaxy](https://galaxy.ansible.com/home).
An example (community) collection to install could be [Ansible Community Docker](https://docs.ansible.com/ansible/latest/collections/community/docker/index.html#plugins-in-community-docker) using the method in [Ansible doc Collection](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html).
You can also install Ansible roles and collections into a local path using '-p'.
You can also install Ansible roles and collections into a specific local path using '-p'.
(ansible2.9.6)$ cd ~/ansible
(ansible2.9.6)$ ansible-galaxy collection install community.docker -p collections
Process install dependency map
Starting collection install process
......@@ -180,16 +181,16 @@ 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
(ansible2.9.6)$ deactivate
Create another Python virtual environment for Ansible 3.0
---------------------------------------------------------
Create another Python virtual environment for Ansible 3.4.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](https://pypi.org/project/ansible/#history):
$ cd ~/ansible
$ python3 -m venv python-venvs/ansible3.4.0
$ . python-venvs/ansible3.4.0/bin/activate
$ . python-venvs/ansible3.4.0/bin/activate #BASH: '.' same as source
(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
......
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