TExcelTAD
Home › Configuration Management: Ansible › Modules & ansible-doc

Modules & ansible-doc

A module is a small program that does one kind of job and reports back in JSON: whether it changed something, whether it failed, and data you can register and use later. Ansible copies the module to the host, runs it with the host's Python and removes it again.

Most modules follow the same pattern: you describe a desired state, and the module first checks the current state, then acts only if the two differ. That is where idempotency comes from: it is built into each module, not into Ansible as a whole.

The modules you will use most

Job Modules (all ansible.builtin. unless noted)
files file, copy, template, lineinfile, blockinfile, replace, stat, slurp, fetch, community.general.ini_file
packages package (generic), apt, dnf, pip
services service, systemd_service
users user, group, ansible.posix.authorized_key
commands command, shell, script, raw
downloads get_url, unarchive, uri (HTTP APIs)
control debug, assert, fail, set_fact, wait_for, pause, meta
system cron, hostname, reboot, ansible.posix.sysctl, ansible.posix.mount, community.general.ufw
reuse include_tasks, import_tasks, include_role, import_role

ansible-doc: the manual is on your machine

ansible-doc shows every module's documentation offline, for exactly the version you have installed, which is more reliable than a web search that lands on the docs for another version.

bash · try it yourself
ansible-doc -l ansible.builtin | grep -E '^ansible.builtin.(copy|lineinfile|template) '
echo "=== the options of lineinfile, from the snippet form"
ansible-doc -s ansible.builtin.lineinfile | grep -oE '^      [a-z_]+:' | tr -d ' :' | paste -sd ' '
ansible.builtin.copy                   Copy files to remote locations
ansible.builtin.lineinfile             Manage lines in text files
ansible.builtin.template               Template a file out to a target host
=== the options of lineinfile, from the snippet form
attributes backrefs backup create encoding firstmatch group insertafter insertbefore line mode owner path regexp search_string selevel serole setype seuser state unsafe_writes validate

Useful variants:

Command Shows
ansible-doc ansible.builtin.copy full page: description, every option with its default, notes, examples, return values
ansible-doc -s <module> a task skeleton with every option
ansible-doc -l / -l <collection> list modules
ansible-doc -t filter -l list filters (also -t lookup, -t callback, -t inventory, -t connection)
ansible-doc -t keyword when playbook keywords

The EXAMPLES section is the fastest way to learn a module. Here is the start of the examples for tempfile:

bash · try it yourself
ansible-doc ansible.builtin.tempfile | sed -n '/^EXAMPLES:/,/^RETURN/p' | head -12
EXAMPLES:
- name: Create temporary build directory
  ansible.builtin.tempfile:
    state: directory
    suffix: build

- name: Create temporary file
  ansible.builtin.tempfile:
    state: file
    suffix: temp
  register: tempfile_1

What a module returns

Every module returns a JSON dictionary. The keys changed and failed are always there; the rest depends on the module and is documented under RETURN VALUES. register stores the result so later tasks can use it:

bash · try it yourself
mkdir demo && echo "hello" > demo/greeting.txt
cat > stat.yml <<'YML'
- name: Inspect a file
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Stat the file
      ansible.builtin.stat:
        path: demo/greeting.txt
        checksum_algorithm: sha256
      register: st

    - name: Show part of the result
      ansible.builtin.debug:
        msg: "exists={{ st.stat.exists }} size={{ st.stat.size }} mode={{ st.stat.mode }} sha256={{ st.stat.checksum[:12] }}"
YML
ansible-playbook stat.yml | grep -A2 'Show part'
TASK [Show part of the result] *************************************************
ok: [localhost] => {
    "msg": "exists=True size=6 mode=0644 sha256=5891b5b522d5"

Arguments: YAML, not key=value

You will see an older inline style in blog posts:

yaml
- name: Old style, avoid
  file: path=/srv/app state=directory mode=0755

It still works, but types become strings, long lines are hard to review, and ansible-lint complains. Write arguments as a YAML dictionary, as everywhere in this course.

When there is no module

Look in this order: ansible.builtin, the installed collections (ansible-doc -l | grep -i <thing>), Galaxy. If nothing fits, use command with creates/changed_when (next lessons), or write your own module: a Python file in library/ next to the playbook that calls AnsibleModule(...) and exit_json(changed=...).

Test yourself

Where does idempotency in Ansible come from?

  1. The control node keeps a state file
  2. Each module checks the current state before acting
  3. Ansible always rolls back
  4. SSH guarantees it

Which command prints a ready-to-paste task with all options of a module?

  1. ansible-doc -l
  2. ansible-doc -s <module>
  3. ansible-config dump
  4. ansible-galaxy info

Which two keys does every module result contain?

  1. stdout and rc
  2. changed and failed
  3. msg and item
  4. host and time