A file in a home directory has the SELinux type user_home_t, but Samba needs samba_share_t. How do you fix that with Ansible, and why might the fix not last?
ansible.builtin.file with setype: samba_share_t changes the type like chcon; but that only labels the file, so a relabel (restorecon) puts the old type back. A permanent change needs a policy rule, for example with community.general.sefcontext.
SELinux (Security-Enhanced Linux) gives every file a context (user:role:type:level), and access rules are written per type. ls -Z shows it:
$ ls -Z samba_file
-rw-r--r--. owner group unconfined_u:object_r:user_home_t:s0 samba_file
Samba may only share files of type samba_share_t, so with user_home_t the share is refused even though normal permissions allow it.
- name: SELinux type is set to samba_share_t
ansible.builtin.file:
path: /path/to/samba_file
setype: samba_share_t
Afterwards ls -Z shows ...:object_r:samba_share_t:s0.
This is the equivalent of chcon: it changes the label on disk but not the policy that says what the label should be. restorecon or a full relabel resets the file to the policy's default (user_home_t in a home directory). To make it stick, add a file-context rule (community.general.sefcontext, the module behind semanage fcontext) and apply it, or use the redhat.rhel_system_roles.selinux role.
Go deeper:
community.general.sefcontext module (official docs) — persistent SELinux file-context rules.
SELinux (Wikipedia) — contexts, types and policies.