Ask Your Question

How to enable/start a service based on a fact?

asked 2017-02-23 11:29:10 -0600

EricL gravatar image

updated 2017-02-23 11:32:07 -0600

Hi, I'm working on RedHat Enterprise Linux 7 (systemd) and would like to enable and start the 'fstrim.timer' service only if the host is physical. I don't see a way within the service resource to do this. I guess I could use an exec to enable and start the service onlyif the 'facter is_virtual' command returns false (as shown below - this works) But that seems very un-Puppet like. I also do not want to run the exec every time the Puppet agent runs.

puppet apply -e "exec { 'fstrim.timer': path => '/bin:/usr/bin:/usr/sbin', 
command => 'systemctl enable fstrim.timer; systemctl start fstrim.timer',
onlyif => 'test `facter is_virtual` == "false"'}"

Thanks, Eric

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-02-23 13:11:50 -0600

puser gravatar image

updated 2017-02-24 07:24:58 -0600

Hey Eric,

It's Eric :). When I do a facter I see the following: virtual => vmware Do you not see this? I am using facter 3.3.0

EDIT 1 or more simply: facter -p virtual

EDIT 2 When I try facter -p virtual on an LPAR I do not get anything back. Perhaps key off that value if it is undef?

EDIT 3 When I try facter -p virtual on an HP physical (and IBM) I get physical back.

I would get a list of your unix servers and do some kind of a loop through them, something like:

for i in `cat test.list`; do ssh $i 'echo "`hostname`:`facter -p virtual`"'; done

(test first ^)

Then dump that in excel, and parse the data by ':' then filter on both columns and you can see which values to filter on. virtual => vmware (or whatever else) .

EDIT 4 facter 3.3.0

if ( $::virtual != "vmware" ) #depending on your virtual provider
     service { 'fstrim.timer':
     ensure => running,
     enable => true,
edit flag offensive delete link more


EDIT 4 is what I needed, thank you. For some reason I could not get that if stmt to work on the command line but it works perfectly in my module. Thanks!

EricL gravatar imageEricL ( 2017-02-23 17:34:01 -0600 )edit

answered 2017-02-24 07:03:11 -0600

natemccurdy gravatar image

updated 2017-02-24 07:03:37 -0600

Facts are available to use in Puppet code. Here's a snippet of code that does what you are looking for:

if $facts['is_virtual'] == false {

  service { 'fstrim.timer':
    ensure => running,
    enable => true,


Note that in the example above I'm using the $facts hash, which is only available as of Puppet 3.5 and only enabled by default in Puppet 4. If you're below that, or using open-source Puppet lower than version 4, replace:



edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-02-23 11:29:10 -0600

Seen: 130 times

Last updated: Feb 24 '17