Back to Lab Listing

Lab Objective:
Install Ansible on Linux and use a playbook to automate a configuration task.

Lab Purpose:
Infrastructure as Code (IaC) means managing and provisioning infrastructure through machine-readable configuration files rather than manual processes. Ansible is one of the most widely used IaC tools — it is agentless, uses YAML playbooks, and can configure hundreds of devices from a single control machine. This lab demonstrates the core Ansible workflow tested in the N10-009 exam.

Lab Tool:
Ubuntu Linux VM (VirtualBox — free). Ansible (free, installed via apt).

Lab Topology:
A single Ubuntu Linux virtual machine. Ansible will manage localhost — no additional machines required.

Lab Walkthrough

Task 1

Open a terminal on your Ubuntu VM. Install Ansible:

sudo apt update
sudo apt install ansible -y

Verify the installation:

ansible --version

The first line will tell you the Ansible version:

ansible [core 2.17.x]
config file = /etc/ansible/ansible.cfg
configured module search path = ...
ansible python module location = ...
executable location = /usr/bin/ansible
python version = 3.10.x ...
jinja version = ...

Task 2

Add localhost to the Ansible inventory so Ansible knows which devices to manage. The second line is all one command:

sudo mkdir -p /etc/ansible
sudo sh -c 'echo "localhost ansible_connection=local" >> /etc/ansible/hosts'

Verify the hosts file was updated:

cat /etc/ansible/hosts

The output should be:

localhost ansible_connection=local

Task 3

Test that Ansible can connect to localhost using the ping module:

ansible all -m ping

You should see a SUCCESS response with “ping”: “pong”. This confirms Ansible can communicate with the managed host.

Expected output:

localhost | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}

SUCCESS = Ansible successfully connected to the machine. “ping”: “pong” = Just like playing ping-pong, the machine replied “I’m here and ready!” This step is important because it proves Ansible is installed correctly and can control your system. If you see SUCCESS and “ping”: “pong”, you’re ready to move on!

Task 4

Run a one-off ad-hoc command using Ansible to check free memory on the managed host:

ansible all -m shell -a 'free -m'

This demonstrates how Ansible can run any shell command across all managed hosts simultaneously — useful for gathering information from many devices at once.

This command tells Ansible to:

  • Connect to all managed hosts (in this lab = just your localhost)
  • Run the Linux command free -m (shows memory in megabytes)
  • Return the result to you

localhost | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 16384 2456 11234 456 2694 13456
Swap: 2048 0 2048

What to look for:

  • CHANGED → Ansible successfully ran the command
  • The memory table showing total, used, free, etc.
  • No error messages

This demonstrates ad-hoc commands — a quick way to run any shell command on one or many machines without writing a full playbook.

Task 5

Now create an Ansible playbook. A playbook is a YAML file that defines a series of tasks to run on managed hosts. Create a file called lab_iac.yml:

nano lab_iac.yml

Enter the following content exactly (get a copy from the Network+ Resources page on 101labs.net):

---
- hosts: all
tasks:
- name: Create a configuration file
copy:
content: "Network+ IaC Lab - Configured by Ansible"
dest: ~/network_config.txt

Save and exit nano with Ctrl+X then Y then Enter.

Task 6

Run the playbook:

ansible-playbook lab_iac.yml

Ansible will show the tasks it is running and whether any changes were made. Look for “changed=1” in the output indicating the file was created.

PLAY [all] *************************************************************************

TASK [Gathering Facts] ************************************************************
ok: [localhost]

TASK [Create a configuration file] ************************************************
changed: [localhost]

PLAY RECAP ************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0

Task 7

Verify the file was created by Ansible:

cat ~/network_config.txt

You should see:

Network+ IaC Lab - Configured by Ansible

Now run the playbook a second time:

ansible-playbook lab_iac.yml

This time you will see “changed=0” — Ansible detected the file already exists with the correct content and made no changes. This is called idempotency — a core IaC principle, meaning you can run the same playbook repeatedly without causing unintended changes.

PLAY RECAP ************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Notes:
Ansible uses YAML playbooks to define the desired state — what the system should look like, rather than step-by-step instructions. This is the declarative approach to IaC. In production environments, Ansible playbooks are stored in version control systems such as GitHub, allowing teams to track changes, roll back configurations, and collaborate. Ansible is agentless — it communicates over SSH and requires no software installed on managed devices beyond Python.

Newsletter Subscribe

Stay Informed, Stay Inspired: Subscribe for Cutting-Edge IT-Certification Insights

101 Labs Newsletter