| 1 | # no-free-form |
| 2 | |
| 3 | This rule identifies any use of |
| 4 | [free-form](https://docs.ansible.com/projects/ansible/latest/index.html#action-shorthand) |
| 5 | module calling syntax and asks for switching to the full syntax. |
| 6 | |
| 7 | **Free-form** syntax, also known as **inline** or **shorthand**, can produce |
| 8 | subtle bugs. It can also prevent editors and IDEs from providing feedback, |
| 9 | autocomplete and validation for the edited line. |
| 10 | |
| 11 | !!! note |
| 12 | |
| 13 | As long you just pass a YAML string that contains a `=` character inside as the |
| 14 | parameter to the action module name, we consider this as using free-form syntax. |
| 15 | Be sure you pass a dictionary to the module, so the free-form parsing is never |
| 16 | triggered. |
| 17 | |
| 18 | As `raw` module only accepts free-form, we trigger `no-free-form[raw]` only if |
| 19 | we detect the presence of `executable=` inside raw calls. We advise the explicit |
| 20 | use of `args:` for configuring the executable to be run. |
| 21 | |
| 22 | This rule can produce messages as: |
| 23 | |
| 24 | - `no-free-form` - Free-form syntax is discouraged. |
| 25 | - `no-free-form[raw-non-string]` - Passing a non-string value to `raw` module is |
| 26 | neither documented nor supported. |
| 27 | |
| 28 | ## Problematic code |
| 29 | |
| 30 | ```yaml |
| 31 | --- |
| 32 | - name: Example with discouraged free-form syntax |
| 33 | hosts: localhost |
| 34 | tasks: |
| 35 | - name: Create a placeholder file |
| 36 | ansible.builtin.command: chdir=/tmp touch foo # <-- don't use free-form |
| 37 | - name: Use raw to echo |
| 38 | ansible.builtin.raw: executable=/bin/bash echo foo # <-- don't use executable= |
| 39 | changed_when: false |
| 40 | ``` |
| 41 | |
| 42 | ## Correct code |
| 43 | |
| 44 | ```yaml |
| 45 | --- |
| 46 | - name: Example that avoids free-form syntax |
| 47 | hosts: localhost |
| 48 | tasks: |
| 49 | - name: Create a placeholder file |
| 50 | ansible.builtin.command: |
| 51 | cmd: touch foo # <-- ansible will not touch it |
| 52 | chdir: /tmp |
| 53 | - name: Use raw to echo |
| 54 | ansible.builtin.raw: echo foo |
| 55 | args: |
| 56 | executable: /bin/bash # <-- explicit is better |
| 57 | changed_when: false |
| 58 | ``` |
| 59 | |
| 60 | !!! note |
| 61 | |
| 62 | This rule can be automatically fixed using [`--fix`](../autofix.md) option. |