Actions
action #175848
opencoordination #161414: [epic] Improved salt based infrastructure management
Validate sls files in salt-{states,pillars}-openqa with a best effort approach
Start date:
2025-01-20
Due date:
% Done:
0%
Estimated time:
Tags:
Description
Motivation¶
We regularly have invalid YAML in our salt repos because we can't easily validate .sls files with jinja tags.
A quite simple script replacing {{ }}
and {% %}
constructs with some dummy values can help us, although it can't catch everything.
Suggestions¶
- Optionally create local yaml files if there is an error so user running it locally can see the generated YAML to better spot the mistake
- Look if state.show_sls can help
- https://github.com/saltstack/salt/issues/802 mentions
salt-call slsutil.renderer
andsalt-call yaml.lint
Updated by tinita 17 days ago ยท Edited
Here's a script that works for our salt repos:
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use YAML::PP;
use Encode;
use Getopt::Long;
GetOptions("replace" => \my $replace);
my $yp = YAML::PP->new( duplicate_keys => 1 );
for my $file (@ARGV) {
open my $fh, '<', $file or die $!;
my $sls = decode_utf8 do { local $/; <$fh> };
close $fh;
if ($replace) {
my $yaml = replace($sls);
print encode_utf8 $yaml;
}
else {
say "================ Validating $file";
validate($file, $sls);
}
}
sub replace {
my ($sls) = @_;
# Remove anything between {% else %} and {% endif %}. Might need
# to include {% elsif %} as well, but works without it so far
$sls =~ s/\{%.? else .?%\}.*?(\{%.? endif .?%\})/$1/gs;
# Now just remove any {% ... %}
$sls =~ s/\{%.*?%\}//g;
# Replace {{ something }} with nothing when it's on its own line
$sls =~ s/^ *\{\{.*?\}\} *$//mg;
# Replace {{ something }} with REPLACEMENT
$sls =~ s/\{\{.*?\}\}/REPLACEMENT/g;
return $sls;
}
sub validate {
my ($file, $sls) = @_;
my $yaml = replace($sls);
my $data = eval { $yp->load_string($yaml) };
if ($@) {
#say $sls;
say "Error ($file):\n$@";
say "Call 'perl $0 --replace $file | yamlpp-highlight' to check the YAML yourself";
}
}
Usage:
find . -name "*.sls" | xargs perl jinja-yaml.pl
Checking a certain file with an error can be done with
perl jinja-yaml.pl openqa/openvswitch.sls --replace | yamlpp-highlight
Updated by okurz 16 days ago
- Tags set to infra, salt, salt-states-openqa
- Project changed from openQA Project (public) to openQA Infrastructure (public)
- Category changed from Feature requests to Feature requests
- Priority changed from Normal to Low
- Target version set to Tools - Next
- Parent task set to #161414
Actions