Install Ansible
In this article we’ll walk through installing Ansible on Debian 12 (Bookworm).
To take advantage of the most up-to-date version of Ansible, we’ll be installing Ansible via Python’s Package Manager (pip) rather than through the built-in apt package manager. This method is admittedly more complicated then using the built-in apt package manager but will allow us to install a more recent version of Ansible.
Directories Preparation
To begin, create a directory which will house our Python Virtual Environment. For this article, we’ll use /opt/virtualenv so it’s accessible to multiple users.
Install acl
Ensure the acl package is installed. We’re using ACLs rather than the traditional unix permissions as its more flexible when it comes to multi-user setups.
sudo apt install aclConfigure Group
Create a group to control access to the /opt/virtualenv directory. For this article we’ll use a group named: ansible.
sudo groupadd ansibleAdd all user’s who will be administering Ansible to this group.
sudo usermod -a -G ansible usernameCreate Directory
Create the directory that will house our Python Virtual Environment.
sudo mkdir /opt/virtualenvConfigure Permissions
Configure the default permissions so the ansible group will have read, write and execute permissions on /opt/virtualenv.
sudo setfacl -m g:ansible:rwx /opt/virtualenvRerun the command, now with the -d flag (for default). This flag configures the default permissions for the directory so all underlying files/folders are given the same permissions when created.
sudo setfacl -d -m g:ansible:rwx /opt/virtualenvRestart Shell
Restart the shell for the changes to take effect.
exec bashInstall Python
Now its time to create our Python Virtual Environment. Install the necessary packages.
sudo apt install python3-pip python3-venvFor folks wishing to manage both Linux and Windows hosts, install these additional packages.
sudo apt install python3-dev gcc libkrb5-dev krb5-user libkrb5-dev sshpassDuring the installation, you’ll be asked to enter information regarding your realm. Feel free to skip this section as it will be covered later on.
Create Virtual Environment
Run the following command to create the Python Virtual Environment. The virtual environment will be stored in /opt/virtualenv.
sudo python3 -m venv /opt/virtualenvActivate Environment
Activate the virtual environment in preparation for installing Ansible.
source /opt/virtualenv/bin/activateIf you ever wish to leave the virtual environment, simply run deactivate.
deactivateAlias (Optional)
Typing out source /opt/virtualenv/bin/activate every time you need to activate the Python Virtual Environment isn’t the most intuitive. To solve this, we can create an alias.
For this article, we’ll use the command activate as it pairs well with the built-in command to drop out of the virtual environment deactivate.
Throw the following to the bottom of your ~/.bashrc file to create the alias.
|
|
Restart your shell to see the changes take effect.
exec bash.bashrc file located under /etc/skel to have it automatically added to every new user of the system.
Now you can simply type activate to activate the virtual environment.
activateTo leave the virtual environment, enter deactivate.
deactivateInstall Ansible
With our virtual environment activated, install Ansible.
pip3 install ansibleIf wishing to manage both Linux and Windows hosts, install these additional packages.
pip3 install requests pywinrm[kerberos]Run ansible --version to confirm it installed successfully.
ansible [core 2.18.1]
config file = None
configured module search path = ['/home/tyler/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /opt/virtualenv/lib/python3.11/site-packages/ansible
ansible collection location = /home/tyler/.ansible/collections:/usr/share/ansible/collections
executable location = /opt/virtualenv/bin/ansible
python version = 3.11.2 (main, Sep 14 2024, 03:00:30) [GCC 12.2.0] (/opt/virtualenv/bin/python3)
jinja version = 3.1.5
libyaml = TrueAnsible is fully installed on the host! Browsing through /opt/virtualenv you’ll see that Ansible is now installed within the virtual environment rather than the usual OS directories.