json in blockvars, routerbackup

This commit is contained in:
ROTTLER Tamas 2023-02-27 17:59:55 +01:00
parent 6329caf4a0
commit 21f9406072
2 changed files with 60 additions and 1 deletions

View File

@ -38,7 +38,11 @@ for (my $i = 0; $i <= $#inventory; $i++) {
next if $line !~ /\S/;
# continuation lines (leading whitespace):
while ($i + 1 <= $#inventory) {
last if $inventory[$i + 1] !~ /^\s+([^# ].*)/;
if ($inventory[$i + 1] =~ /^\s+#/) {
$i++;
next;
}
last if $inventory[$i + 1] !~ /^\s+(\S.*)/;
$i++;
my $cont = $1;
$cont =~ s/\s*$//;
@ -65,6 +69,21 @@ for (my $i = 0; $i <= $#inventory; $i++) {
}
}
%blockvars = ();
if ($vars and $vars =~ s/\s+(\{.*\})//) {
my $jsonstring = $1;
my $jo = JSON::PP->new->relaxed->allow_singlequote->allow_barekey;
my $data;
eval {
$data = $jo->decode($jsonstring);
};
if ($@) {
print STDERR "Invalid JSON in line ". ($i + 1) .": $jsonstring\n";
}
for my $k (keys %$data) {
$blockvars{$k} = $data->{$k};
}
}
if ($vars) {
while ($vars =~ /\s+([a-z0-9_]+)=(\S*)/gci) {
my ($a, $v) = ($1, $2);

40
routerbackup Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash
BACKUP=0
CHECK=0
while getopts "bc" opt; do
case $opt in
b)
BACKUP=1
;;
c)
CHECK=1
;;
\?)
echo "usage: $0 [-c] [-f]" >&2
exit 1
esac
done
if [[ $BACKUP = 0 && $CHECK = 0 ]]; then
echo "no action" >&2
exit 1
fi
outfilter=$(dirname $0)/ansible_outfilter
if [[ $BACKUP > 0 ]]; then
DIFF=/tmp/routerbackup.diff.$$
ansible-playbook -e routerbackup_diff=$DIFF routerbackup.yml | $outfilter
cat $DIFF
rm -f $DIFF
fi
if [[ $CHECK > 0 ]]; then
CHECKFILE=/tmp/routerbackup.check.$$
ansible-playbook -t check -e routerbackup_checkfile=$CHECKFILE routerbackup.yml | $outfilter
cat $CHECKFILE
rm -f $CHECKFILE
fi
# vim: set tabstop=4 shiftwidth=4 expandtab smarttab: