json in blockvars, routerbackup
This commit is contained in:
parent
6329caf4a0
commit
21f9406072
21
dynhosts
21
dynhosts
@ -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
40
routerbackup
Executable 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:
|
||||
Loading…
x
Reference in New Issue
Block a user