What are Ansible modules and how do they execute on target hosts?
Learn what Ansible modules are, how they ship to target hosts, execute remotely, return JSON, and get removed — the core of Ansible's agentless automation.
Expected Interview Answer
Ansible modules are small, self-contained units of code that perform a specific task — installing a package, copying a file, managing a service — and Ansible ships them to the target host, executes them there, then removes them.
When a task runs, Ansible resolves the module, bundles it with the task arguments, transfers it over the connection plugin (usually SSH) to a temporary directory on the target, runs it with the remote Python (or another interpreter for non-Python modules), and captures its JSON output. Most modules are declarative and idempotent — they describe desired state rather than commands — and the control node never persists an agent because the module is deleted after each run.
- Agentless — nothing stays installed on targets
- Declarative desired-state model instead of raw commands
- Return structured JSON that drives changed/ok/failed status
- Reusable building blocks across playbooks and roles
- Thousands of built-in and collection modules cover most tasks
AI Mentor Explanation
Think of the module as a specialist substitute fielder the team management flies in for one over: they arrive with exact instructions, take the catch on the boundary, report back the result, and then leave the ground — no permanent squad spot. Ansible ships the module to the host, it does its one job, returns the scorecard as JSON, and is removed, leaving no agent behind.
Step-by-Step Explanation
Step 1
Task resolution
Ansible parses the task, finds the named module in the search path or collection, and validates its arguments.
Step 2
Bundle and transfer
The module code plus arguments are packaged and copied over the connection plugin (SSH by default) into a temp directory on the target.
Step 3
Remote execution
The module runs on the host using the remote interpreter (Python for most modules), applying the desired state described by the arguments.
Step 4
Return JSON
The module prints a JSON result reporting changed/ok/failed plus any facts, which Ansible reads over the connection.
Step 5
Cleanup
Ansible deletes the temporary module files so nothing persists — this is what makes Ansible agentless.
What Interviewer Expects
- Understanding that modules are shipped, run remotely, then removed
- Awareness of the agentless model and why no daemon persists
- Knowledge that modules return structured JSON
- Distinction between declarative modules and raw command/shell
- Familiarity with the remote interpreter requirement (usually Python)
Common Mistakes
- Claiming Ansible installs a permanent agent on targets
- Confusing modules with plugins (connection, lookup, filter)
- Thinking modules run on the control node rather than the target
- Overusing command/shell instead of purpose-built idempotent modules
- Forgetting that most modules need Python on the managed host
Best Answer (HR Friendly)
“Ansible modules are small ready-made programs that each do one job, like installing software or copying a file. Ansible sends the right module to the server, runs it there, gets back a report of what happened, and then removes it — so nothing needs to stay installed on the machines you manage.”
Code Example
- name: Ensure nginx is installed and running
hosts: web
become: true
tasks:
- name: Install nginx package
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Ensure nginx service is running
ansible.builtin.service:
name: nginx
state: started
enabled: true# Ping module ships to hosts, runs, returns JSON, then is removed
ansible web -m ansible.builtin.ping
# Copy module transfers a file and reports changed/ok
ansible web -m ansible.builtin.copy \
-a "src=/etc/hosts dest=/tmp/hosts mode=0644"Follow-up Questions
- What is the difference between a module and a plugin in Ansible?
- Why does Ansible need Python on the managed host for most modules?
- How does the command module differ from the shell module?
- What are collections and how do they package modules?
- How would you write a custom module?
MCQ Practice
1. Where does an Ansible module actually execute?
Ansible copies the module to the target, runs it there with the remote interpreter, then removes it — the control node only orchestrates.
2. What does a module return to Ansible after it runs?
Modules print JSON that Ansible parses to determine changed/ok/failed and to collect any returned data or facts.
3. Which statement about Ansible's execution model is correct?
Ansible is agentless: modules are transferred, executed, and deleted, so no persistent agent remains on managed hosts.
Flash Cards
What is an Ansible module? — A small, self-contained unit of code that performs one task and returns JSON, shipped to and run on the target host.
Why is Ansible called agentless? — Modules are copied to the host, run, and deleted after each task — no persistent daemon is installed.
What interpreter runs most modules? — The remote host's Python interpreter, though some modules use PowerShell or other languages.
How does Ansible know a task's result? — The module prints structured JSON reporting changed/ok/failed plus any facts, read back over the connection.