Ansible modules are self-contained scripts that can be used by Ansible to execute specific tasks on managed nodes. Modules can be written in any language that understands JSON and either XML or YAML formatting. They are used in playbooks, roles, and templates.
For example, a common module for managing user accounts would look something like this:
---
- name: Manage user accounts
hosts: all
tasks:
- name: Create user
user:
name: "{{ username }}"
state: present
groups: admin
shell: /bin/bash
- name: Delete user
user:
name: "{{ username }}"
state: absent
By using modules in playbooks, you can save time and reduce the amount of code needed to manage complex systems. This makes Ansible a highly efficient tool for automating configuration and system management.