question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable auto-switching between yaml and ansible language

See original GitHub issue

Problem

As one of the most common confusions with vscode extension is the fact that vscode often fails to decide which document language to use between generic yaml and ansible language. That is because at this moment only filename patterns are use to determine if a file uses one language or another. Once user manually change the language the IDE remembers the selection, but our goal is to avoid the need for this manual selection.

With @ganeshrn help, we identified several existing extensions that hook into document open event and decide to change the language of the document based on the file content, such the shebang line for example:

Solution

We need to implement the same hook for document open even and when the current document is using either yaml or ansible, use a detection function that is guessing if the file is a playbook or a taskfile.

We currently have the isPlaybook function but we need a new one named detectDocumentLanguage which receives the open document and returns the name of the vscode language that vscode should use for it.

The function should return null if we were not able to make a decision. For example if current document is not YAML or the YAML loading fails, we are expected to return null, as we cannot make a decision.

Once the document is loaded, the logic should be:

  • if current document language is not already ansible or yaml, return null.
  • if document is null, empty list, string or empty mapping, return null (inconclusive)
  • if document is a non empty mapping, return yaml
  • if document is a non empty sequence, check if the first element is a dictionary having either one of the following keys: name, hosts, import_playbook, return ansible.
  • return null as fallback, as we do not want to auto-switch document type if we are not confident enough.

Alternatives

No response

Additional context

Please look at current extensions that do this switching as all of them are open source and the implementation for document open is quite easy.

This feature does not require us to touch ansible-language-server project, as this hook is specific to vscode client.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
ssbarneacommented, Aug 4, 2022

Yeah, not required. But when someone shots himself in the foot, you cannot blame the weapon. In fact ansible can also load files with any extensions, yml or yaml not being required, but that does not mean we can support anything that happens to work.

Once we fix the auto-detection users will be VERY happy!

0reactions
MarkusTeufelbergercommented, Sep 21, 2022

Would it be possible to apply the schemata for ansible content to y(a)ml files as a strong hint about their “language”? A file that doesn’t match them could still be an ansible file, but ones that match very likely are true positives.

So far it still seems like a file such as roles/tasks/main.yml will likely be miscategorized as yaml as it doesn’t refer to hosts or import_playbook.

Would a patch to extend https://github.com/ansible/vscode-ansible/blob/038b856fe98cab77adc7f73ca728d93474c29cd4/package.json#L297-L301 be welcome that at least adds "**/tasks/*.y(a)ml" and "**/handlers/*.y(a)ml"?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding YAML for Ansible | Enable Sysadmin - Red Hat
Ansible playbooks are written in YAML, YAML Ain't Markup Language. Understanding YAML syntax is a key to success with Ansible.
Read more >
YAML Syntax - Ansible Documentation
This page provides a basic overview of correct YAML syntax, which is how Ansible playbooks (our configuration management language) are expressed.
Read more >
Uses a specific YAML file as an inventory source.
You are reading the latest (stable) community version of the Ansible documentation. If you are a Red Hat customer, refer to the Ansible...
Read more >
Ansible playbooks
Playbooks are expressed in YAML format with a minimum of syntax. ... a clever playbook available to configure ansible-pull through a crontab from...
Read more >
Deep dive on Ansible VScode extension
The goal of the protocol is to allow programming language support to ... The extension also depends on the Red Hat vscode-yaml extension...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found