LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 10 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

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 host
  • fetch: bring a file from the host back to the control node
  • file: create, remove or set attributes of files, directories and links
  • lineinfile / blockinfile: edit one line or one block inside an existing file
  • stat: read a file's status without changing it

File modules grouped by direction: copy and template push to the host, fetch pulls back, file, lineinfile and blockinfile change the host in place, stat only reads

* 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:

or press any other key

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:

or press any other key