Create variable as result of command execution

159 views
Skip to first unread message

Roman Alekseev

unread,
Mar 19, 2014, 3:27:15 AM3/19/14
to puppet...@googlegroups.com
Hello,

What is the best way in puppet to create a variable as result of command below:
$nginx_dir=`/usr/sbin/nginx -V 2> /etc/puppet/nginx.args | /bin/cat /etc/puppet/nginx.args | /usr/bin/awk -F'--http-proxy-temp-path=' {'print $2'} | /usr/bin/awk {'print $1'}  | /bin/grep '\/'`

Any advices will be appreciated.

José Luis Ledesma

unread,
Mar 19, 2014, 4:13:16 AM3/19/14
to puppet...@googlegroups.com

Hi,

  I think that the best way is a custom fact.

Regards

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/7211e50a-32db-4c68-953e-164edecc1932%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roman Alekseev

unread,
Mar 19, 2014, 5:03:26 AM3/19/14
to puppet...@googlegroups.com
Hi,

I created fact but it doesn't work. What did I make wrong?

# nginx_arg.rb

Facter.add("nginx_arg") do
  setcode do
    Facter::Util::Resolution.exec("/usr/sbin/nginx -V 2> /etc/puppet/nginx.args | /bin/cat /etc/puppet/nginx.args | /usr/bin/awk -F'--http-proxy-temp-path=' {'print $2'} | /usr/bin/awk {'print $1'}  | /bin/grep '\/'")
  end
end


Thanks.

среда, 19 марта 2014 г., 12:13:16 UTC+4 пользователь Jose Luis Ledesma написал:

Roman Alekseev

unread,
Mar 19, 2014, 9:17:58 AM3/19/14
to puppet...@googlegroups.com
Solved:

****************************

# nginx_arg.rb

Facter.add("nginx_arg") do
  setcode do
        Facter::Util::Resolution.exec('/usr/sbin/nginx -V 2>&1  | /bin/grep -oP "http-proxy-temp-path=.*?\s" | /usr/bin/awk -F\'=\' {\'print $2\'}')
  end
end
****************************

среда, 19 марта 2014 г., 11:27:15 UTC+4 пользователь Roman Alekseev написал:

jcbollinger

unread,
Mar 19, 2014, 9:31:45 AM3/19/14
to puppet...@googlegroups.com


On Wednesday, March 19, 2014 4:03:26 AM UTC-5, Roman Alekseev wrote:
Hi,

I created fact but it doesn't work. What did I make wrong?

# nginx_arg.rb

Facter.add("nginx_arg") do
  setcode do
    Facter::Util::Resolution.exec("/usr/sbin/nginx -V 2> /etc/puppet/nginx.args | /bin/cat /etc/puppet/nginx.args | /usr/bin/awk -F'--http-proxy-temp-path=' {'print $2'} | /usr/bin/awk {'print $1'}  | /bin/grep '\/'")
  end
end




There are a couple of weird things about that, but before that:
  • What does "doesn't work" mean, specifically?
  • Where is the file located on the master?
  • Do you have pluginsync enabled on the agents?
  • Is the fact value output by "facter -p" on agent nodes that have synced the fact?
With respect to the command itself,
  • which output stream from /usr/sbin/nginx are you trying to use?  (You are using stderr (via a temp file), but you are also piping stdout and ignoring it.)
  • the unquoted curly braces in the command will be parsed by the shell, not passed to awk.
  • Setting awk's field separator to '--http-proxy-temp-path=' will probably not do anything sensible if there are other options listed after than one in nginx's output.
  • If you need a temp file, you should put it in /tmp or /var/tmp and give it an unpredictable name.  But you don't need one for what it looks like you're trying to do.
Were I writing a command to do what I think yours is trying to do, it would look something like this:

    Facter::Util::Resolution.exec(
      "/usr/sbin/nginx -V 2>&1 1>/dev/null | /bin/sed -n 's,.*--http-proxy-temp-path=\(/\S\+\).*,\1,p'"
    )

That pipes the stderr stream of the nginx command into sed, discarding the stdout.  Sed then filters out everything except any strings of non-whitespace beginning with a slash character (/) and directly following the string "--http-proxy-temp-path=".  The output does not include the "--http-proxy-temp-path=".  If you need to allow for whitespace either before or after the '=' character then the pattern can be adjusted to achieve that.


John

Roman Alekseev

unread,
Mar 21, 2014, 12:38:25 AM3/21/14
to puppet...@googlegroups.com
Thank you!

This is also working:


/usr/sbin/nginx -V 2>&1  | /bin/grep -oP "http-proxy-temp-path=.*?\s" | /usr/bin/awk -F\'=\' {\'print $2\'}

среда, 19 марта 2014 г., 17:31:45 UTC+4 пользователь jcbollinger написал:
Reply all
Reply to author
Forward
0 new messages