mschuchard / linter-ansible-syntax

ansible syntax check linter for pulsar
MIT License
5 stars 3 forks source link

ansible version detect still broken #9

Closed walterrowe closed 4 years ago

walterrowe commented 4 years ago

This is related to issue #7. The version detect is still broken 11 months later.

% /usr/local/bin/ansible-playbook --version
ansible-playbook 2.9.11
  config file = /Users/wrowe/git-repos/examples/ansible.cfg
  configured module search path = ['/Users/wrowe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.9.11/libexec/lib/python3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.5 (default, Jul 21 2020, 10:48:26) [Clang 11.0.3 (clang-1103.0.32.62)]

% which ansible-lint                       
/usr/local/bin/ansible-lint
% /usr/local/bin/ansible-lint --version
ansible-lint 4.2.0

% pip3 show ansible
WARNING: Package(s) not found: ansible

% brew info python3
python@3.8: stable 3.8.5 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python@3.8/3.8.5 (4,422 files, 68.4MB) *
  Poured from bottle on 2020-07-24 at 11:55:40
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python@3.8.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✔, openssl@1.1 ✔, readline ✔, sqlite ✔, xz ✔
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python@3.8/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

% brew info ansible
ansible: stable 2.9.11 (bottled), HEAD
Automate deployment, configuration, and upgrading
https://www.ansible.com/
/usr/local/Cellar/ansible/2.9.11 (18,085 files, 223MB) *
  Poured from bottle on 2020-08-07 at 09:54:15
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/ansible.rb
License: GPL-3.0
==> Dependencies
Build: pkg-config ✔
Required: libyaml ✔, openssl@1.1 ✔, python@3.8 ✔
==> Options
--HEAD
    Install HEAD version

I am using atom.io and ansible on macOS. ansible is installed with brew. I can confirm that the proposed solution in #7 works for me.

Screen Shot 2020-08-07 at 10 00 10 AM
mschuchard commented 4 years ago

Could you show your ansible-playbook --help so I can see what about your environment is incompatible with the version check?

walterrowe commented 4 years ago

Sure.

% ansible-playbook --help
usage: ansible-playbook [-h] [--version] [-v] [-k] [--private-key PRIVATE_KEY_FILE] [-u REMOTE_USER] [-c CONNECTION]
                        [-T TIMEOUT] [--ssh-common-args SSH_COMMON_ARGS] [--sftp-extra-args SFTP_EXTRA_ARGS]
                        [--scp-extra-args SCP_EXTRA_ARGS] [--ssh-extra-args SSH_EXTRA_ARGS] [--force-handlers]
                        [--flush-cache] [-b] [--become-method BECOME_METHOD] [--become-user BECOME_USER] [-K]
                        [-t TAGS] [--skip-tags SKIP_TAGS] [-C] [--syntax-check] [-D] [-i INVENTORY] [--list-hosts]
                        [-l SUBSET] [-e EXTRA_VARS] [--vault-id VAULT_IDS]
                        [--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES] [-f FORKS] [-M MODULE_PATH]
                        [--list-tasks] [--list-tags] [--step] [--start-at-task START_AT_TASK]
                        playbook [playbook ...]

Runs Ansible playbooks, executing the defined tasks on the targeted hosts.

positional arguments:
  playbook              Playbook(s)

optional arguments:
  --ask-vault-pass      ask for vault password
  --flush-cache         clear the fact cache for every host in inventory
  --force-handlers      run handlers even if a task fails
  --list-hosts          outputs a list of matching hosts; does not execute anything else
  --list-tags           list all available tags
  --list-tasks          list all tasks that would be executed
  --skip-tags SKIP_TAGS
                        only run plays and tasks whose tags do not match these values
  --start-at-task START_AT_TASK
                        start the playbook at the task matching this name
  --step                one-step-at-a-time: confirm each task before running
  --syntax-check        perform a syntax check on the playbook, but do not execute it
  --vault-id VAULT_IDS  the vault identity to use
  --vault-password-file VAULT_PASSWORD_FILES
                        vault password file
  --version             show program's version number, config file location, configured module search path, module
                        location, executable location and exit
  -C, --check           don't make any changes; instead, try to predict some of the changes that may occur
  -D, --diff            when changing (small) files and templates, show the differences in those files; works great
                        with --check
  -M MODULE_PATH, --module-path MODULE_PATH
                        prepend colon-separated path(s) to module library
                        (default=~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules)
  -e EXTRA_VARS, --extra-vars EXTRA_VARS
                        set additional variables as key=value or YAML/JSON, if filename prepend with @
  -f FORKS, --forks FORKS
                        specify number of parallel processes to use (default=5)
  -h, --help            show this help message and exit
  -i INVENTORY, --inventory INVENTORY, --inventory-file INVENTORY
                        specify inventory host path or comma separated host list. --inventory-file is deprecated
  -l SUBSET, --limit SUBSET
                        further limit selected hosts to an additional pattern
  -t TAGS, --tags TAGS  only run plays and tasks tagged with these values
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)

Connection Options:
  control as whom and how to connect to hosts

  --private-key PRIVATE_KEY_FILE, --key-file PRIVATE_KEY_FILE
                        use this file to authenticate the connection
  --scp-extra-args SCP_EXTRA_ARGS
                        specify extra arguments to pass to scp only (e.g. -l)
  --sftp-extra-args SFTP_EXTRA_ARGS
                        specify extra arguments to pass to sftp only (e.g. -f, -l)
  --ssh-common-args SSH_COMMON_ARGS
                        specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand)
  --ssh-extra-args SSH_EXTRA_ARGS
                        specify extra arguments to pass to ssh only (e.g. -R)
  -T TIMEOUT, --timeout TIMEOUT
                        override the connection timeout in seconds (default=10)
  -c CONNECTION, --connection CONNECTION
                        connection type to use (default=smart)
  -k, --ask-pass        ask for connection password
  -u REMOTE_USER, --user REMOTE_USER
                        connect as this user (default=None)

Privilege Escalation Options:
  control how and which user you become as on target hosts

  --become-method BECOME_METHOD
                        privilege escalation method to use (default=sudo), use `ansible-doc -t become -l` to list
                        valid choices.
  --become-user BECOME_USER
                        run operations as this user (default=root)
  -K, --ask-become-pass
                        ask for privilege escalation password
  -b, --become          run operations with become (does not imply password prompting)
walterrowe commented 4 years ago

This code is working.

  // activate linter
  activate() {
    const helpers = require('atom-linter');

    // check for ansible-lint >= minimum version
    helpers.exec(atom.config.get('linter-ansible-syntax.ansibleExecutablePath'), ['--version']).then(output => {
      if (/ansible-playbook 1\.\d+\.\d+/.exec(output)) {
        atom.notifications.addWarning(
          'ansible < 2.0 is unsupported. Backwards compatibility may exist, but is not guaranteed.',
          {
            detail: "Please upgrade your version of ansible to >= 2.0.\n",
            dismissable: true
          }
        );
      }
    });
  },
walterrowe commented 4 years ago

I suspect that in this output:

% /usr/local/bin/ansible-playbook --version
ansible-playbook 2.9.11
  config file = /Users/wrowe/git-repos/examples/ansible.cfg
  configured module search path = ['/Users/wrowe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.9.11/libexec/lib/python3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.5 (default, Jul 21 2020, 10:48:26) [Clang 11.0.3 (clang-1103.0.32.62)]

that the code is picking up the clang-1103.0.32.62 line as a non-empty string causing the version check to be true.

If you include the ansible-playbook string in the check, it limits which line is checked.

mschuchard commented 4 years ago

Based on your output of ansible-playbook --help, the code should be working, as it is functionally identical to my testing environment where it does work correctly.

I also took your output and the tester regexp, and processed it through an online JS regexp tester, and the results were correct.

I am completely unsure what is causing this edge case, but I need to be able to find some way to reproduce it.

walterrowe commented 4 years ago

My solution was proposed in #7. If the proposed solution works, can you just use it? Maybe the online JS regexp tester you used isn't very good?

walterrowe commented 4 years ago

This regex test site shows it picks up the line I suspected.

https://regex101.com/

regex:

1\.\d+\.\d+

string:

ansible-playbook 2.9.11
  config file = /Users/wrowe/git-repos/examples/ansible.cfg
  configured module search path = ['/Users/wrowe/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.9.11/libexec/lib/python3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.5 (default, Jul 21 2020, 10:48:26) [Clang 11.0.3 (clang-1103.0.32.62)]

result:

Match 1

Full match | 432-437 | 1.0.3
Screen Shot 2020-08-07 at 2 36 17 PM

If I add ansible-playbook to the regex, it finds nothing.

regex:

ansible-playbook 1\.\d+\.\d+

result:

Your regular expression does not match the subject string.
walterrowe commented 4 years ago

These sites produce the same result as regex101.

https://www.regexpal.com/ https://www.regextester.com/?fam=118168

Screen Shot 2020-08-07 at 2 43 18 PM
walterrowe commented 4 years ago

What command are you running? If you are still running ansible-playbook --version then you will not have --become in the output.

walterrowe commented 4 years ago

I see the problem. The code downloaded to my system is version 1.2.3 and has code the runs ansible-playbook --version and looks just for the 1.x with a faulty regex.

When I copy the two lines of code from this repository that run ansible-playbook --help and looks for !/--become/, it works.

I originally downloaded the code from atom.io. Perhaps the version on atom.io is outdated.

https://atom.io/packages/linter-ansible-syntax

https://github.com/mschuchard/linter-ansible-syntax/releases/tag/v1.2.3

My apologies for the confusion.

I still argue that explicitly looking at the output from ansible-playbook --version is more bulletproof than looking for whether a specific option is listed in the help.

mschuchard commented 4 years ago

I meant to check the tags for if I had released a new version, but it seems I had forgotten to do so. I assumed I had released a new version in the last several months, but that was incorrect.

Currently a new release is pending a lack of time to fix some issue that recent versions of Ansible are having with one of the error types.

This issue being resolved in upstream does not negate a potential improvement from #9. I will close this and the discussion around that PR can begin now that the context is clear.

walterrowe commented 4 years ago

It is still using the --help option vs --version. The --help option does not provide the version number.

I created a new PR that changes --help to --version, and includes 0 and 1 version check.

Should be bullet proof now.

sandikata commented 1 year ago

Hello,

this issue not seems to be fixed.

ansible-lint < 5.0 is unsupported. Backwards compatibility should exist, but is not guaranteed.
Please upgrade your version of ansible-lint to >= 5.0.

However

$ ansible-playbook --version
ansible-playbook [core 2.13.7]
  config file = None
  configured module search path = ['/home/rosen/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.10/site-packages/ansible
  ansible collection location = /home/rosen/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/lib/python-exec/python3.10/ansible-playbook
  python version = 3.10.9 (main, Dec  7 2022, 10:29:49) [GCC 12.2.1 20221203]
  jinja version = 3.1.2
  libyaml = True
walterrowe commented 1 year ago

@sandikata .. atom is no longer developed. I don't know if this package is still being maintained.

mschuchard commented 1 year ago

@sandikata That is a different package; note the different names in the Github repo and the error message. You probably encountered the issue caused by a backwards incompatible update in ansible-lint that was fixed earlier this year, and so an update to the Atom package will fix your issue. Otherwise, please file an issue at the repo for the Atom package.