vscode-ansible icon indicating copy to clipboard operation
vscode-ansible copied to clipboard

Provide a way to set the working directory for the linters

Open dhoogfr opened this issue 1 year ago • 8 comments

Problem

The plugin runs ansible-lint / ansible --syntax-check from a working directory that seems to be set to the workspace root directory. This can cause issues if for example you have the following directory structure

./project
├── code
│   ├── collections
│   ├── files
│   ├── inventories
│   ├── library
│   ├── playbooks
│   └── roles
└── keys

With the vscode workspace root directory set to "./project" and you have an ansible.cfg file under the code directory.

In such a situation, the linter will not pickup this ansible.cfg file. In my case it causes an error with "ansible-playbook --syntax-check" as the vault-password-file variable is not picked up and it fails to decrypt the vault file:

Unexpected error code 1 from execution of: ansible-playbook --syntax-check -i localhost, code/playbooks/config_sftp_s3fs.ymlansible-lint[internal-error]

Solution

The Python plugin provides a configuration parameter to set the working directory for the linter. A similar option for the Ansible plugin to set a (relative) path would solve the above problems

Alternatives

An alternative would be to have an explicit configuration option to specify the vault password file. But that would not solve the issue with not reading the ansible.cfg file.

Another alternative could be to instead of using the vscode workspace root as working directory, allow the vscode buildin terminal CWD configuration parameter to override it. But that could break existing setups

Additional context

No response

dhoogfr avatar May 10 '23 20:05 dhoogfr

There is a setting to set the command line paramters for ansible-lint, I'm curious if that could be used to set the target directory for linting as a workspace setting

"ansible.validation.lint.arguments": ""

It's also in the extentsion's settings:

image

Let me know if that helps?

TY, brad

cidrblock avatar May 17 '23 12:05 cidrblock

Hi,

I tried to use that setting with the --project-dir parameter, but it did not work. From the error message it seems that this parameter is not passed to the "ansible-playbook --syntax-check" command

image

image

dhoogfr avatar May 17 '23 13:05 dhoogfr

Similarly to @dhoogfr, I've attempted to specify a project directory, including through a .ansible-lint file in the playbook's root folder. I even lobbed in an extra var in the hope that I might hit an undocumented feature 😉:

---
project_dir: .

enable_list:
  - args
  - empty-string-compare
  - no-log-password
  - no-same-owner
  - name[prefix]
  - yaml

extra_vars:
  vault_password_file: ~/vault-password

But like @dhoogfr, the only way I could get the extension to pick up the vault password file was to place an ansible.cfg file in the root of my workspace. That's not a fix if the playbooks under that root use different vault passwords.

Can we set a per-project environment variable, and override ANSIBLE_CONFIG? Could the extension offer that possibility? 🤔 (I'm not sure if the environment variable is merged with any config files...?)

For reference, my project structure is like this (simplified):

Ansible
├── project1
│      .ansible-lint
│      ansible.cfg <--- ignored by the extension
│      inventory
│      main.yml
├── project2
│      .ansible-lint
│      ansible.cfg <--- ignored by the extension
│      inventory
│      main.yml
ansible.cfg <--- used by the extension

I also tried:

  • Launching VS Code with ANSIBLE_CONFIG set (ANSIBLE_CONFIG="[default] vault_password_file = ~/vault-password" code)
  • Using a modeline in the playbook (# code: language=ansible env=ANSIBLE_CONFIG="[default] vault_password_file = ~/vault-password")

Unfortunately I don't think we'll get folder-specific settings from VS Code any time soon, so the best option is for this to be available as a setting within the extension.

PS This issue might need the "waiting on author" tag removing?

robpomeroy avatar Jun 06 '23 14:06 robpomeroy

@robpomeroy The extension does support multi-root workspace so you can try adding project1 and project2 folder at the root of the workspace using Add folder to workspace option and try setting folder level settings.

Screenshot 2023-06-06 at 8 11 05 PM

ganeshrn avatar Jun 06 '23 14:06 ganeshrn

@dhoogfr As mentioned in above comment extension support multi root workspace, so you can add multiple folders at the top level of the workspace.

ganeshrn avatar Jun 06 '23 14:06 ganeshrn

@robpomeroy The extension does support multi-root workspace so you can try adding project1 and project2 folder at the root of the workspace using Add folder to workspace option and try setting folder level settings.

Yes, thank you, I had spotted that. 👍 Unfortunately my workspace is large, structured and mature, and I was hoping not to have to rearrange it. It's already multi-root, with Ansible, Docker, Polyglot development, Packer, Terraform and WSL and some others all as separate roots (eight in total). Within the Ansible root, I have 37 separate projects. Ideally I'd prefer a fix that doesn't involve a complete rearrange. 🙂

robpomeroy avatar Jun 06 '23 14:06 robpomeroy

The Python plugin provides a configuration parameter to set the working directory for the linter. A similar option for the Ansible plugin to set a (relative) path would solve the above problems

I like the idea however in case of single Ansible root folder workspace the setting needs to be changed based on which project you are working on within the VSCode window.

I usually work with separate VSCode windows for different projects and use multi-root workspace for related sub-projects

ganeshrn avatar Jun 06 '23 15:06 ganeshrn

I too have found myself in need of this feature, I have a project/repo with a structure like:

/services/
 └ ansible/
  ├ ansible.cfg
  ├ .yamllint
  ├ .ansible-lint
  ├ .ansible-lint-ignore
  ├ playbooks/
  └ roles/

Currently, the VSCode Ansible extension only works when opened from /services/ansible and not / or /services/. Watching the extension debug output I see that the .ansible-lint config file is used, but it does not detect the ansible.cfg config, nor .yamllint.

I tried setting ANSIBLE_CONFIG in the workspace .vscode/settings.json:

"terminal.integrated.env.linux": {
    "ANSIBLE_CONFIG": "/xxx/services/ansible/ansible.cfg"
}

But this did not fix anything.

I found that if I set in the console export ANSIBLE_CONFIG=/xxx/services/ansible/ansible.cfg and then open vscode like code . then the ansible extension detects the ansible.cfg and works as I would expect (however the .yamllint config file still is not used).

Assuming the issue is detection of ansible.cfg then one of the following would be really helpful.

  1. Respect the vscode. internal terminal (SP?) environment setting (ANSIBLE_CONFIG)
  2. Add a config setting for the path to the ansible.cfg
  3. Add a config setting for the ansible working directory

erichiller avatar Jul 25 '24 12:07 erichiller