← again tap: answer good → ↑ swipe up at the end: next
Question
Which file-management modules does ansible.builtin provide, and what is each one for?
Answer
copy: put a file from the control node on the hostfetch: bring a file from the host back to the control nodefile: create, remove or set attributes of files, directories and linkslineinfile/blockinfile: edit one line or one block inside an existing filestat: read a file's status without changing it
* Sort the modules by direction: push, pull, change in place, or only look. *
The modules ship with ansible-core in the ansible.builtin collection, so they are always available. A useful way to remember them is by the direction the data moves:
| Module | Direction | Typical job |
|---|---|---|
copy |
control node → host | deploy a ready-made file; can also set owner, mode and SELinux context |
fetch |
host → control node | collect a log, a key or a config from many hosts, sorted by host name |
file |
on the host | touch, mkdir, rm, chmod, chown, symlinks, SELinux attributes |
lineinfile |
on the host | make sure one line is present, replaced or removed |
blockinfile |
on the host | manage a multi-line block between marker comments |
stat |
on the host, read-only | does it exist, what mode, what checksum? |
Several of them accept the same attribute options as file (owner, group, mode, setype, ...), so "copy and set permissions" is one task, not two.
Go deeper:
ansible.builtin.file module (official docs) — the attribute options the other file modules share.
Question
Which extra file modules come from the ansible.posix collection, and when do you reach for synchronize instead of copy?
Answer
ansible.posix.patch applies patches with GNU patch, and ansible.posix.synchronize wraps rsync. Use synchronize for many files or whole directory trees, where rsync only transfers what changed.
ansible.posix is a separate collection (part of the Ansible package, not of bare ansible-core), so the full name ansible.posix.synchronize is required.
- name: synchronize local file to remote files
ansible.posix.synchronize:
src: file
dest: /path/to/file
How synchronize differs from copy:
copy |
synchronize |
|
|---|---|---|
| Engine | Ansible transfers each file | runs rsync |
| Needs on the hosts | Python only | rsync on both ends |
| Good for | single files, small sets | big trees, incremental updates, delete of extra files |
| Coverage | full Ansible attributes | the common rsync options, not all of them |
By default the "local host" is where the task runs, usually the control node, and the target is the managed host (mode: push). mode: pull turns the direction around. For rsync features the module doesn't expose, the docs suggest calling rsync with the command module.
Go deeper:
ansible.posix.synchronize module (official docs) —
mode,delete,rsync_optsand the delegation rules.rsync (Wikipedia) — the delta-transfer algorithm behind it.