Skip to content
EgyKode
Guided lab

Ansible Architecture, Configuration & Automated Inventory

39 minIntermediate

This creates billable resources. Run it in a dev environment and destroy it when you finish. Set a budget alarm first.

Success criteria

0 of 3

What you are building#

What is Ansible Configuration & Dynamic Inventory?#

Ansible is an open-source, agentless configuration management and automation platform. Unlike legacy agent-based systems (such as Chef, Puppet, or SaltStack) that require a background daemon agent installed on every target node, Ansible operates over standard SSH (TCP Port 22) on Linux or WinRM on Windows.

  • Control Node: The central workstation or CI server running Ansible binaries. It reads playbooks, parses dynamic inventory inputs, compiles Python module payloads, opens SSH sessions, and executes modules in-memory on remote target hosts.
  • Target Managed Node: The target server (our Jenkins EC2 host deployed in Lab 06). It requires zero custom agent software — only an SSH daemon (sshd) and a standard Python 3 runtime environment.
  • Dynamic Inventory: In modern elastic cloud infrastructure (AWS), EC2 instance IP addresses change dynamically whenever instances are recreated. A dynamic inventory script queries infrastructure outputs (e.g. terraform output -raw jenkins_public_ip) and dynamically generates Ansible's inventory file (inventory/hosts.ini), completely eliminating manual IP editing.
text
                                ANSILE AGENTLESS INVENTORY ARCHITECTURE
                                
  +-------------------------------------+                 +-------------------------------------+
  |         ANSIBLE CONTROL NODE        |                 |      AWS EC2 TARGET MANAGED NODE    |
  |                                     |                 |  (Jenkins Server — Public Subnet A) |
  |  [ master config: ansible.cfg ]     |                 |                                     |
  |  [ script: update-inventory.sh ]    |   SSH Port 22   |  - Standard OpenSSH Daemon (sshd)   |
  |        | (Fetches TF Output IP)     | ==============> |  - Python 3 Runtime Environment     |
  |        v                            |  Pushes Python  |  - Zero Agent Software Installed    |
  |  [ inventory: hosts.ini ]           |  Module Payload |  - Systemd Service Manager          |
  +-------------------------------------+                 +-------------------------------------+

Steps#

Step 1: Make Script Executable#

Terminal
cd 02-Configuration-Management-Ansible/Lab07-Ansible-Inventory-Setup
chmod +x scripts/update-inventory.sh

Step 2: Extract Public IP from Lab 06 & Generate Inventory#

Terminal
JENKINS_IP=$(terraform -chdir=../../01-Infrastructure-Terraform/Lab06-Jenkins-EC2-AWSBackup output -raw jenkins_public_ip)
./scripts/update-inventory.sh $JENKINS_IP

Step 3: Test Ad-Hoc Ping Connectivity#

Terminal
ansible jenkins_servers -m ping -i inventory/hosts.ini

Verify it worked#

Terminal
ansible jenkins_servers -m ping -i inventory/hosts.ini

Expected Terminal Output:

yaml
jenkins_host:
  changed: false
  ping: pong

Verify inventory/hosts.ini contents:

Terminal
cat inventory/hosts.ini

Expected File Output:

ini
[jenkins_servers]
jenkins_host ansible_host=54.210.142.88 ansible_user=ubuntu
 
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_common_args='-o StrictHostKeyChecking=no'


Clean up#

Run this even if you did not finish. Everything above is destroyable, and an account full of half-built experiments is how a surprise bill starts.

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
terraform destroy -auto-approve
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId'

Cost of this lab: Free tier — Ansible itself is free; the cost is whichever EC2 instances it configures. A t3.micro control node is inside the free tier.

The concept behind it

Ready to try it without help?Do the challenge