Create a variable out of a bash command and notify it

95 views
Skip to first unread message

Nir Fishler

unread,
Dec 4, 2022, 10:43:07 AM12/4/22
to Puppet Users
Hello,

I'd like to store an output of a bash command into a variable, and then print it to stdout using notify in Puppet.

Main goal: get a list of upgrade-able packages and notify the user about it.

I have tried the below but it doesn't seem to work:

$pkgs_list = $("/usr/bin/apt --simulate upgrade | grep '^Inst' | awk -F ' ' '{print $2}'")

notify { "STDOUT: ${pkgs_list}": }

Martin Alfke

unread,
Dec 5, 2022, 2:33:55 AM12/5/22
to puppet...@googlegroups.com
Hi Nir,

You want to collect information on nodes and you want to use this information in a catalog.
This is what facter is used for. 

<module>/lib/facter/upgradable_packages.rb
Facter.add(:upgradable_packages) do
  setcode do
    Facter::Core::Execution.execute("/usr/bin/apt --simulate upgrade | grep '^Inst' | awk -F ' ' '{print $2}'")
  end
end

Within your puppet code you can then refer to the fact:

notify { $upgradable_packages: }

Hth,
Martin

--
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/9793f643-c386-4400-9fba-fd42f3e1269dn%40googlegroups.com.

Dirk Heinrichs

unread,
Dec 5, 2022, 2:47:52 AM12/5/22
to puppet...@googlegroups.com
Am Sonntag, dem 04.12.2022 um 07:43 -0800 schrieb Nir Fishler:

Main goal: get a list of upgrade-able packages and notify the user about it.

The puppetlabs-apt module from the Puppet Forge has various facts for this.

HTH...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Nir Fishler

unread,
Dec 5, 2022, 3:02:09 AM12/5/22
to Puppet Users
Thanks a lot guys for your prompt reply!

Gonna try it out later on.

Nir Fishler

unread,
Dec 18, 2022, 8:10:42 AM12/18/22
to Puppet Users
@Martin,

I have tried your approach; I put the facter file underneath:
/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/custom_facts/core/execution/

file:
username.rb

content:
# username.rb

Facter.add(:username) do
  setcode do
    Facter::Core::Execution.execute("/usr/bin/hostname |/usr/bin/awk -F'-' {'print $1'}")
  end
end

But I keep getting an error:
Error: Evaluation Error: Empty string title at 0. Title strings must have a length greater than zero. (line: 1, column: 8) on node puppet-master.domain.com

what am I missing here?

Dirk Heinrichs

unread,
Dec 19, 2022, 1:34:17 AM12/19/22
to puppet...@googlegroups.com
Am Sonntag, dem 18.12.2022 um 05:10 -0800 schrieb Nir Fishler:

/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/custom_facts/core/execution/

This is the wrong place. Ruby based facts should be put into "lib/facter/" inside a module, see https://puppet.com/docs/puppet/7/custom_facts.html.
Reply all
Reply to author
Forward
0 new messages