Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive and bad auto --fix for command-instead-of-shell #4386

Closed
wimglenn opened this issue Nov 4, 2024 · 2 comments
Closed

False positive and bad auto --fix for command-instead-of-shell #4386

wimglenn opened this issue Nov 4, 2024 · 2 comments
Labels

Comments

@wimglenn
Copy link
Contributor

wimglenn commented Nov 4, 2024

Summary

In this task, if there was a pre-existing crontab it is considered a (non-fatal) failure mode:

- name: Get pre-existing crontab (if any)
  ansible.builtin.shell: "! crontab -l"
  changed_when: false
  register: missing_crontab
  ignore_errors: true # don't stop the rest of the playbook

We invert the return code (i.e. we want rc=0 when there is no existing crontab) using POSIX shell ! operator.

ansible-lint is unhappy and wants the task to use ansible.builtin.command instead of shell:

command-instead-of-shell: Use shell only when shell functionality is required.

The auto-fixer makes a suggestion which doesn't actually work:

- name: Get pre-existing crontab (if any)
  ansible.builtin.command: "! crontab -l"
  changed_when: false
  register: missing_crontab
  ignore_errors: true # don't stop the rest of the playbook

This "fixed" task fails, it appears to be trying to run a program called literally "!":

TASK [Get pre-existing crontab (if any)] ***********************
fatal: [examplehost]: FAILED! => changed=false 
  cmd: '''!'' crontab -l'
  msg: '[Errno 2] No such file or directory: b''!'': b''!'''
  rc: 2
  stderr: ''
  stderr_lines: <omitted>
  stdout: ''
  stdout_lines: <omitted>
...ignoring

Workaround: use ansible.builtin.command: bash -c "! crontab -l"

Issue Type
  • Bug Report
OS / ENVIRONMENT
$ ansible-lint --version
ansible-lint 24.9.2 using ansible-core:2.16.11 ansible-compat:24.9.1 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip
@wimglenn wimglenn added bug new Triage required labels Nov 4, 2024
@audgirka audgirka removed the new Triage required label Nov 13, 2024
@audgirka
Copy link
Contributor

@wimglenn Thanks for the bug, a PR would be welcome.

wimglenn added a commit to wimglenn/ansible-lint that referenced this issue Nov 14, 2024
@wimglenn
Copy link
Contributor Author

Added #4403. It failed because it doesn't have a label, but I don't seem to have permission to add labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

2 participants