DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

LAB: Understanding Playbooks & Agentless Architecture

Objective

  • Prove Ansible is agentless
  • Understand playbook structure
  • See fact gathering
  • Observe idempotency
  • Understand execution order

Architecture

Image

Image

Image

Image

Mac (Control Node)
↓ SSH
EC2 Ubuntu (Managed Node)

No agent installed on EC2.


Step 1 — Prove It Is Agentless

SSH into EC2:

ssh -i ~/Downloads/pem/key.pem ubuntu@3.141.22.178
Enter fullscreen mode Exit fullscreen mode

Now check:

ps aux | grep ansible
Enter fullscreen mode Exit fullscreen mode

You will see:

Nothing running.

Ask students:

“If Ansible works, where is the agent?”

Answer:
There is none.

Ansible connects via SSH, executes modules remotely, and exits.

Exit EC2.


Step 2 — Create Playbook to See Execution Flow

Create:

vim playbook-demo.yml
Enter fullscreen mode Exit fullscreen mode

Paste:

- name: Playbook Behavior Demo
  hosts: web
  become: yes

  tasks:

    - name: Show hostname
      command: hostname

    - name: Create test file
      file:
        path: /tmp/ansible-test.txt
        state: touch

    - name: Write content into file
      copy:
        dest: /tmp/ansible-test.txt
        content: "Hello from Ansible"

    - name: Install htop
      apt:
        name: htop
        state: present
Enter fullscreen mode Exit fullscreen mode

Step 3 — Run Playbook

ansible-playbook -i inventory.ini playbook-demo.yml
Enter fullscreen mode Exit fullscreen mode

Students observe:

  1. Gathering Facts
  2. Tasks executed in order
  3. changed vs ok

Step 4 — Run It Again

Run again:

ansible-playbook -i inventory.ini playbook-demo.yml
Enter fullscreen mode Exit fullscreen mode

Now students see:

  • hostname → ok
  • file → ok
  • copy → ok
  • htop → ok
  • changed = 0

Ask them:

“Did Ansible re-install htop?”

Answer:
No. Because state was already correct.

This is idempotency.


Step 5 — Disable Fact Gathering

Now modify playbook:

Add:

gather_facts: no
Enter fullscreen mode Exit fullscreen mode

Like this:

- name: Playbook Behavior Demo
  hosts: web
  become: yes
  gather_facts: no
Enter fullscreen mode Exit fullscreen mode

Run again.

Students will see:
No "Gathering Facts" step.

Explain:

Ansible automatically collects system info using setup module.


Step 6 — Observe SSH Activity

Run with verbose mode:

ansible-playbook -i inventory.ini playbook-demo.yml -vvv
Enter fullscreen mode Exit fullscreen mode

Students will see:

  • SSH connection
  • Module transfer
  • Execution
  • Cleanup

Explain:

Ansible copies module temporarily → executes → removes it.

That proves:

Agentless + push-based.


Step 7 — Break Something Intentionally

SSH into EC2:

sudo rm /tmp/ansible-test.txt
Enter fullscreen mode Exit fullscreen mode

Run playbook again.

Now students see:

changed=1

Explain:

Ansible detected missing state and corrected it.

Top comments (0)