For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/ansible-risky-file-permissions.md.
A documentation index is available at /llms.txt.
Files and directories created or modified by Ansible tasks must have explicit, least-privilege file modes. Omitting the mode or relying on preserved/default permissions can leave artifacts world-readable or writable, increasing the risk of data exposure and privilege escalation.
This rule checks file-related modules—archive, assemble, copy, file, get_url, template (including their FQCNs) and content-creation modules like htpasswd and ini_file. Tasks that create files (task state not absent/link) without defining the mode property are flagged.
For modules that provide a create boolean (for example htpasswd and ini_file), tasks where create is true (or defaults to true) and mode is not defined are also flagged. mode: preserve is only allowed for copy and template modules. Any other module using mode: preserve is reported as invalid.
To remediate, add an explicit mode with an appropriate octal value, or set create: false when creation is not desired.
- name:Create config file with restrictive permissionsansible.builtin.file:path:/etc/myapp/config.ymlstate:filemode:'0640'- name:Create ini file with explicit modecommunity.general.ini_file:path:/etc/myapp/settings.inicreate:truemode:'0640'
Compliant Code Examples
---- name:SUCCESS_PERMISSIONS_PRESENThosts:alltasks:- name:Permissions not missing and numericansible.builtin.file:path:foomode:"0600"---- name:SUCCESS_PERMISSIONS_PRESENT_GET_URLhosts:alltasks:- name:Permissions not missing and numericansible.builtin.get_url:url:http://foodest:foomode:"0600"---- name:SUCCESS_ABSENT_STATEhosts:alltasks:- name:Permissions missing while state is absent is fineansible.builtin.file:path:foostate:absent---- name:SUCCESS_DEFAULT_STATEhosts:alltasks:- name:Permissions missing while state is file (default) is fineansible.builtin.file:path:foo---- name:SUCCESS_LINK_STATEhosts:alltasks:- name:Permissions missing while state is link is fineansible.builtin.file:path:foo2src:foostate:link---- name:SUCCESS_CREATE_FALSEhosts:alltasks:- name:File edit when create is falseansible.builtin.lineinfile:path:foocreate:falseline:some content here---- name:SUCCESS_REPLACEhosts:alltasks:- name:Replace should not require modeansible.builtin.replace:path:fooregexp:foo---- name:SUCCESS_RECURSEhosts:alltasks:- name:File with recursive does not require modeansible.builtin.file:state:directoryrecurse:truepath:foo- name:Permissions not missing and numeric (fqcn)ansible.builtin.file:path:barmode:"755"- name:File edit when create is false (fqcn)ansible.builtin.lineinfile:path:foocreate:falseline:some content here---- name:LINIINFILE_CREATEtasks:- name:create is true 2xlineinfile:path:fooline:some content heremode:"0600"---- name:PRESERVE_MODEtasks:- name:not preserve valuecopy:path:foomode:preserve---- name:LINEINFILE_CREATE2tasks:- name:create_falseansible.builtin.lineinfile:path:foocreate:trueline:some content heremode:"644"