Installing Ansible and Checking the Python Interpreter
In this first step, we will install Ansible and examine the default Python interpreter it uses. This will help us understand the base configuration before we make any changes.
Installing Ansible
Let's start by installing Ansible on the system:
sudo apt update
sudo apt install -y ansible
This installs the latest version of Ansible available in the Ubuntu repositories. After installation completes, we can verify that Ansible was installed correctly by checking its version:
ansible --version
You should see output similar to this:
ansible [core 2.12.0]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.10.x (default, Ubuntu 22.04) [GCC 11.2.0]
jinja version = 3.0.3
libyaml = True
Notice that the output includes the Python version being used. This is important information as it tells us which Python interpreter Ansible is currently configured to use.
Creating a Simple Inventory File
For Ansible to work, we need an inventory file that lists the hosts we want to manage. Let's create a simple inventory file:
- In the WebIDE, create a new file by clicking on the "New File" icon in the Explorer panel
- Name the file
inventory.ini
- Add the following content to the file:
[local]
localhost ansible_connection=local
This inventory file defines a group called local
with just one host - localhost
- and specifies that we want to connect to it directly rather than via SSH.
Checking the Python Interpreter on the Target
Now let's check which Python interpreter Ansible will use on our target host:
ansible -i inventory.ini local -m setup -a "filter=ansible_python*"
This command runs the Ansible setup module which gathers facts about the host, filtering for information related to Python. You should see output containing details about the Python interpreter being used:
localhost | SUCCESS => {
"ansible_facts": {
"ansible_python": {
"executable": "/usr/bin/python3",
"has_sslcontext": true,
"type": "cpython",
"version": {
"major": 3,
"micro": 10,
"minor": 10,
"releaselevel": "final",
"serial": 0
},
"version_info": [
3,
10,
10,
"final",
0
]
},
"ansible_python_version": "3.10.10"
},
"changed": false
}
This confirms that Ansible is using Python 3 on the target host. By default, Ansible tries to use the best available Python interpreter on the target system.