Introduction
Ansible, a powerful automation tool, enables you to manage configurations, deploy software, and automate various tasks in a structured and organized manner. However, to harness the full potential of Ansible, it’s essential to follow best practices and avoid potential pitfalls. In this article, we’ll explore Ansible Error 202, “risky-octal”, in [Ansible-Lint](/articles/ansible-lint)which focuses on the use of octal file permissions in your Ansible playbooks. We’ll discuss why using integers or octal values in YAML can lead to unexpected behavior and how to ensure that your file permissions are defined safely and predictably.
The Problem: Risky Octal File Permissions
Ansible Error 202, “risky-octal”, is designed to prevent the use of octal file permissions in a non-standard form, which can result in unpredictable outcomes. Octal file permissions are typically written with a leading zero (e.g., 0644). When you omit the leading zero and use an integer (e.g., 644), the YAML parser interprets the value differently, leading to unexpected results.
Problematic Code Example:
``yaml
---
- name: Example playbook
hosts: localhost
tasks:
- name: Unsafe example of declaring Numeric file permissions
ansible.builtin.file:
path: /etc/foo.conf
owner: foo
group: foo
mode: 644 # <- Risky octal without a leading zero
`
In the above code snippet, the “mode” parameter lacks the leading zero in the octal permission, making it prone to unpredictable behavior.
Output:
`bash
WARNING Listing 3 violation(s) that are fatal
risky-octal: mode: 644 should have a string value with leading zero mode: "01204" or use symbolic mode.
202.yml:5 Task/Handler: Unsafe example of declaring Numeric file permissions
yaml[new-line-at-end-of-file]: No new line character at the end of file
202.yml:11
yaml[trailing-spaces]: Trailing spaces
202.yml:11
Read documentation for instructions on how to ignore specific rule violations.
Rule Violation Summary
count tag profile rule associated tags
1 yaml[new-line-at-end-of-file] basic formatting, yaml
1 yaml[trailing-spaces] basic formatting, yaml
1 risky-octal safety formatting
Failed: 3 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'min'.
``
Modules that are checked
- [ansible.builtin.assemble](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assemble_module.html)
- [ansible.builtin.copy](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html)
- [ansible.builtin.file](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html)
- [ansible.builtin.replace](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html)
- [ansible.builtin.template](https://docs.ansible.com/ansib