AnsibleModule & Argument Spec
Define and validate module inputs.
AnsibleModule & Argument Spec is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet AnsibleModule
The AnsibleModule class is your toolkit. It parses the arguments Ansible sends, validates them, and gives you helpers to return results.
from ansible.module_utils.basic import AnsibleModuleThe argument_spec Map
You describe every input your module accepts in an argument_spec dictionary. Ansible reads it to validate what users pass in.
argument_spec=dict(
name=dict(type='str', required=True)
)Give Each Argument a Type
Set a type like str, int, bool, list, or dict. Ansible coerces and rejects bad input before your code even runs.
count=dict(type='int', default=1)Mark What Is Required
Add required=True and Ansible fails with a clear error if the user forgets that argument. No defensive checking needed.
path=dict(type='str', required=True)Provide Sensible Defaults
Use default so optional arguments get a value automatically. Users only set what they want to change.
state=dict(type='str', default='present')Restrict to Valid Choices
The choices list limits an argument to allowed values. Anything else is rejected up front with a helpful message.
state=dict(choices=['present', 'absent'])Build the Module Object
Pass your spec into AnsibleModule to create the object. From here on, all your inputs live safely in module.params.
module = AnsibleModule(argument_spec=argument_spec)Read Validated Inputs
Access your arguments through module.params, a plain dictionary. The values are already typed and validated, so just use them.
name = module.params['name']Cross-Argument Rules
Need two arguments together or never both? Pass rules like required_together or mutually_exclusive to AnsibleModule and it enforces them.
mutually_exclusive=[['src', 'content']]Hide Secrets with no_log
Mark sensitive inputs with no_log=True so passwords never appear in Ansible logs or verbose output.
password=dict(type='str', no_log=True)Let the Spec Do the Work
A good argument_spec replaces piles of manual validation. Describe inputs once and Ansible handles types, errors, and docs for free.
Quick Check
Think about how user inputs reach your module code.
Recap
Define inputs in argument_spec with types, defaults, required, and choices, build AnsibleModule, then read clean values from module.params. ✅
Frequently asked questions
Is the “AnsibleModule & Argument Spec” lesson free?
Yes — the full text of “AnsibleModule & Argument Spec” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “AnsibleModule & Argument Spec”?
Define and validate module inputs. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “AnsibleModule & Argument Spec” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- When to Build a Custom Module
- AnsibleModule & Argument Spec
- Return JSON & Honor changed
- Support Check Mode in Your Module