Hi Guys,

So we have ~ 2k machines configured via Puppet.
These have some ssh keys set that are now redundant and need removing

I inherited the following (minimized) code
define line($file, $line, $ensure = 'present') { case $ensure { default: { err ( "unknown ensure value ${ensure}" ) } present: { exec { "/bin/echo '${line}' >> '${file}'": unless => "/bin/grep -qFx -- '${line}' + '${file}'" } } absent: { exec { "/usr/bin/perl -ni -e 'print unless /^\ +\Q${line}\\E\$/' '${file}'": onlyif => "/bin/grep -qFx -- '${line}' + '${file}'" } } } } line { 'ssh_key' : file => '/root/user/b1.bak', line => 'ssh-rsa AAAA/BBBB/knMQ== user@host.f.q.d.n', ensure => absent, }

but it seems it can't cope with special chars like '/'/ & '@'; test run shows
2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Having no space bet +ween pattern and following word is deprecated at -e line 1. 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Bareword found wher +e operator expected at -e line 1, near "/^\Qssh-rsa AAAA/BBBB" 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): (Missing operat +or before BBBB?) 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Array found where o +perator expected at -e line 1, at end of line 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): (Missing operat +or before ?) 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Bareword found wher +e operator expected at -e line 1, near "q.d.n" 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Backslash found whe +re operator expected at -e line 1, near "n\" 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): syntax error at -e +line 1, near "/^\Qssh-rsa AAAA/BBBB" 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (notice): Execution of -e abo +rted due to compilation errors. 2019-11-27 21:49:14 +0000 Puppet (err): /usr/bin/perl -ni -e 'print un +less /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f.q.d.n\E$/' '/root/user/ +b1.bak' returned 255 instead of one of [0] 2019-11-27 21:49:14 +0000 /Stage[main]/Main/Line[ssh_key]/Exec[/usr/bi +n/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/knMQ== user@host.f. +q.d.n\E$/' '/root/user/b1.bak']/returns (err): change from notrun to +0 failed: /usr/bin/perl -ni -e 'print unless /^\Qssh-rsa AAAA/BBBB/kn +MQ== user@host.f.q.d.n\E$/' '/root/user/b1.bak' returned 255 instead +of one of [0]

I have tried various ways of quoting/escaping etc, but none seem to work.
I would definitely appreciate a working solution

Cheers
Chris

In reply to One liner: remove ssh keys with quotemeta by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.