Ansible Fundamentals · Module 03 of 12

Install & verify

Create a clean Ansible project, install Ansible in a virtual environment and verify a safe lab before making changes.

Learning objectives

By the end of this module, you should be able to:

  • Install Ansible without polluting the system Python environment.
  • Verify the installed version and configuration.
  • Create a small project directory and test host access safely.

Use an isolated Python environment

For a learning workstation, a Python virtual environment keeps the Ansible installation isolated from operating-system Python packages. This makes the lab easier to reproduce and remove.

The Ansible community package contains ansible-core plus a curated set of collections. ansible-core contains the execution engine and ansible.builtin content. Which one you install depends on the environment you intend to manage; for this beginner course the community Ansible package is convenient.

Make the project reproducible

Keep inventory, playbooks, templates and role content in one project directory that can later be placed under source control. A predictable layout makes it easier to test changes and share automation with a team.

Verify before changing anything

A good first validation sequence is: check ansible --version, validate inventory, confirm normal SSH, then run ansible.builtin.ping. Only after transport and inventory are proven should you run state-changing tasks.

Worked examples

See the idea in practice.

Install in a virtual environment

python3 -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip
pip install ansible
ansible --version
  • The virtual environment isolates Python packages.
  • Activating it puts its ansible command first in PATH.
  • ansible --version shows ansible-core, Python and configuration paths.

Minimal project layout

ansible-lab/
├── inventory.ini
├── ansible.cfg
├── playbooks/
├── templates/
└── roles/
  • Keep automation artifacts together.
  • Do not store passwords or private keys in the project.
Hands-on lab

Lab — Build a clean control-node project

Goal: Create a minimal Ansible workspace and prove that the toolchain works.

STEP 1

Create the project

mkdir -p ~/ansible-lab/playbooks ~/ansible-lab/templates ~/ansible-lab/roles
cd ~/ansible-lab
STEP 2

Create and activate a virtual environment

python3 -m venv .venv
. .venv/bin/activate
pip install ansible
STEP 3

Inspect the installation

ansible --version
ansible-config dump --only-changed

Verify

  • ansible --version completes successfully.
  • The command reports the expected Python environment.
  • The project directory contains no credentials.

Expected outcome

  • Ansible is available only when the intended environment is active.
  • No managed-host changes have been made yet.

If it fails

  • If python3 -m venv is unavailable, install the OS package that provides Python virtual-environment support.
  • If pip installs to an unexpected interpreter, use python -m pip from inside the active environment.
Real-world connection

Teams pin automation dependencies in controlled execution environments so that a playbook behaves consistently in CI/CD, laptops and automation platforms.

Avoid these traps

Common mistakes

  • Installing arbitrary Python packages into the system interpreter on production-oriented hosts.
  • Starting with write operations before proving SSH and inventory.
  • Committing private keys, vault passwords or plaintext credentials.
Knowledge check

Can you explain it without looking back?

Open each answer only after you have tried to answer the question yourself.

1Why use a Python virtual environment for a lab?

It isolates dependencies and makes the setup easier to reproduce and remove.

2What should you verify before running state-changing tasks?

The Ansible installation, inventory, SSH connectivity and basic Ansible module execution.

3Should private SSH keys live in the Ansible project directory?

No. Treat keys and secrets separately from version-controlled automation content.