-
Hi, In the older version of Ansible Lint, I was using
Since the signature of Is it possible to receive the entire text for a particular file, to look for a specific line somewhere in the file? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I am sorry that there is no clear documentation on the AnsibleLintRule methods, they did suffer a lot of refactoring and there will still be a little bit more to do. Your best friend is searching for current usage in internal rules. Basically you can still use the same method now, the difference is that Lintable object has few attributes as Keep in mind that regardless the name of the method, it will be called for any file kind, regardless if is yaml or not. My desire was to rename it but there were already too many breaking changes in v5, so it was postponed. I would also like to mention that I am very open to receive new rule contributions to the linter itself. Your rule can easily be modified to be generic purpose and opt-in, by adding a config option like |
Beta Was this translation helpful? Give feedback.
-
An update for anyone else who had this issue, thanks to @ssbarnea pointing me in the right direction I was able to get our custom rule working again. Here's a simpler version of it to demonstrate the concept: import re
from typing import List
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule
class MatchStringInLineRule(AnsibleLintRule):
id = '<id goes here>'
shortdesc = '<short description goes here>'
description = '<description goes here>'
severity = 'LOW'
tags = ['formatting']
done: List[str] = []
def matchyaml(self, file: Lintable) -> List[MatchError]:
# No errors initially.
result: List[MatchError] = []
# Return the empty result set (no errors) if this is not a playbook.
if file.kind != 'playbook':
return result
text_lines = file._content.split('\n')
for line_num, line in enumerate(text_lines):
if re.match("<regex goes here>", line):
# We have a match! Return the empty result set (no errors).
return result
# If we make it here, we didn't find any matches, so add a matcherror to the results set and return.
error_message = 'Required text not found anywhere in the playbook.'
result.append(self.create_matcherror(linenumber=len(text_lines), filename=file_path, details=error_message))
self.done.append(file_path)
return result |
Beta Was this translation helpful? Give feedback.
An update for anyone else who had this issue, thanks to @ssbarnea pointing me in the right direction I was able to get our custom rule working again. Here's a simpler version of it to demonstrate the concept: