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

ansible-lint reports key-duplicates in EXAMPLE doc of plugins #3860

Open
guidograzioli opened this issue Oct 20, 2023 · 10 comments
Open

ansible-lint reports key-duplicates in EXAMPLE doc of plugins #3860

guidograzioli opened this issue Oct 20, 2023 · 10 comments
Labels

Comments

@guidograzioli
Copy link

Summary

ansible-lint 2.21.0 reports duplicate-keys inside EXAMPLE documentation of plugins

Issue Type
  • Bug Report
OS / ENVIRONMENT
ansible-lint --version
ansible-lint 6.21.0 using ansible-core:2.15.5 ansible-compat:4.1.10 ruamel-yaml:0.17.35 ruamel-yaml-clib:0.2.8
  • ansible installation method: pip
  • ansible-lint installation method: pip
STEPS TO REPRODUCE

Run ansible-lint against a plugin with the following code:

EXAMPLES = """
# Filename must end with kubevirt.[yml|yaml]

# Authenticate with token, and return all VirtualMachineInstances for all accessible namespaces
plugin: kubevirt.core.kubevirt
connections:
- host: https://192.168.64.4:8443
  api_key: xxxxxxxxxxxxxxxx
  validate_certs: false

# Use default config (~/.kube/config) file and active context, and return VirtualMachineInstances
# from namespace testing with interfaces connected to network bridge-network
plugin: kubevirt.core.kubevirt
connections:
- namespaces:
  - testing
  network_name: bridge-network
Desired Behavior

Should not report an en error

Actual Behavior

Reports an error: yaml[key-duplicates]: Duplication of key "plugin" in mapping

https://github.com/kubevirt/kubevirt.core/actions/runs/6570606188/job/17848302926#step:6:93

@guidograzioli guidograzioli added bug new Triage required labels Oct 20, 2023
@ssbarnea
Copy link
Member

That is a genuine error as the entire EXAMPLES is no longer a valid YAML file. Still, I do not know what is the correct way to include more than one example...

@cidrblock
Copy link
Contributor

From the docs:

"The DOCUMENTATION block must be valid YAML."

and for the examples:

"Here you show users how your module works with real-world examples in multi-line plain-text YAML format. The best examples are ready for the user to copy and paste into a playbook."

This is the way I think about the examples, which is why linting them is so important, they should be cut-and-paste ready for the user.

WRT the example above, that's a tricky one :) Examples are generally assumed to playbook/tasks in which case they are a list and we don't have to worry about duplicate keys.

But for 2 examples of an inventory plugin, each of which is a dictionary it makes sense why it was done like it was.

In this case, the error could be ignored or maybe it would pass with a yaml doc seperator between them, which is probably more accurate because these are 2 examples of the same file.

(I haven't tested the doc seperator)

@ssbarnea
Copy link
Member

@Qalthos I think that we might want to modify the current implementation to allow it to use multi-documents. What we need to do is to split the multi-document into multiple file before linting the example, that is because ansible itself does not allow loading of multi-document files.

That is also opening few other questions regarding how can we guess/infer the nature of the example (file kind). I guess we could ask our users to add magic comments? The alternative is to only support tasks and playbooks and determine it based on what we find (dictionary or list).

@guidograzioli
Copy link
Author

Is there a way to disable the rule for DOCUMENTATION and EXAMPLE only? duplicated-key is a very important rule but for those two strings, I'd disable it and forget forever

@ssbarnea ssbarnea changed the title ansible-lint 2.2 reports key-duplicates in EXAMPLE doc of plugins ansible-lint reports key-duplicates in EXAMPLE doc of plugins Oct 24, 2023
@ssbarnea
Copy link
Member

I think that excluding the plugin file should work.

@ssbarnea ssbarnea removed the new Triage required label Oct 25, 2023
@tremble
Copy link

tremble commented Dec 5, 2023

For anyone else hitting this, if you use the document separator (---) between each of the examples, then with ansible-lint 6.22.1 duplicate key errors only fire if there's a duplicate key within the example:

eg

EXAMPLES = r""" 
---

# Example using groups to assign the running hosts to a group based on vpc_id
plugin: amazon.aws.aws_ec2
profile: aws_profile
# Populate inventory with instances in these regions
regions:
  - us-east-2
filters:
  # All instances with their state as `running`
  instance-state-name: running
keyed_groups:
  - prefix: tag
    key: tags
compose:
  ansible_host: public_dns_name
groups:
  libvpc: vpc_id == 'vpc-####'

---

# Define prefix and suffix for host variables coming from AWS.
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
hostvars_prefix: 'aws_'
hostvars_suffix: '_ec2'
"""

unfortunately ansible-test's sanity checks then complain

ERROR: plugins/inventory/aws_ec2.py:157:1: unparsable-with-libyaml: expected a single document in the stream - but found another document

@bcoca
Copy link
Member

bcoca commented Dec 5, 2023

o longer a valid YAML file. Still, I do not know what is the correct way to include more than one example...

it is not required to be YAML, EXAMPLES is treated as text by ansible-doc.

@atocko
Copy link

atocko commented Jan 9, 2024

It is still an issue for the EXAMPLES of dynamic inventory plugins. Those cant have - name blocks.
False positives on multiple examples:

# Example 1
---
plugin: "kind.of.inventory"
param_one: value1

# Example 2
plugin: "kind.of.inventory"
param_one: value2

Error: yaml[key-duplicates]: Duplication of key "plugin" in mapping

Interesting, how to avoid it...

@valkiriaaquatica
Copy link

The yaml[key-duplicates]: Duplication of key "plugin" in mapping error was solved as @tremble said using --- between different plugin examples.

But then using ansible-test sanity received:
ERROR: Found 1 yamllint issue(s) which need to be resolved:
ERROR: plugins/inventory/inventory.py:73:1: unparsable-with-libyaml: expected a single document in the stream - but found another document

@tremble
Copy link

tremble commented May 14, 2024

ERROR: plugins/inventory/inventory.py:73:1: unparsable-with-libyaml: expected a single document in the stream - but found another document

This is a problem with ansible-test. It's been fixed in Ansible Core 2.17 (currently in release candidate stage).

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

No branches or pull requests

7 participants