passing resource ensure=> values in variables?

162 views
Skip to first unread message

Christopher Wood

unread,
Apr 30, 2015, 2:00:45 PM4/30/15
to puppet...@googlegroups.com
I'm trying to pass ensure=> values in variables (and set resource defaults conditionally) and it doesn't seem to work that way. If there's some way of getting this working I am very happy to hear about it.

In my site.pp I have this sort of thing:

if $::devstyle and str2bool($::devstyle) {
notify { 'et': message => 'devstyle on' }

File {
ensure => present,
}
$filefe = present
}
else {
$filefe = file
}

The idea is that puppet will treat all file resources as ensure=>present anywhere that devstyle=true (it's an external fact). I do see the notify text in syslog when the agent run proceeds.

However, two things are not happening:

1) not getting the resource default when the resource does not have ensure=>
(puppet reverts my manually modified file)

2) ensure => $::filefe is not ensuring 'present' when devstyle=true (special corporate bits censored out):

notify { 'mrt1':
message => "filefe=${::filefe}"
}

file { '/etc/mrouted.conf':
ensure => $::filefe,
content => "whatever\n",
}

The notify reports the expected value of $::filefe is as I expect, but the file's contents are still managed.

Apr 30 13:25:30 myhost puppet-agent[28675]: (/Stage[main]/Mrouted/Notify[mrt1]/message) defined 'message' as 'filefe=present'

Apr 30 13:25:26 myhost puppet-agent[28675]: (/Stage[main]/Mrouted/File[/etc/mrouted.conf]/content) content changed '{md5}35195dd2d3f40d5bbd647c642919c86a' to '{md5}f5c4f579df6e0d5b088f8811f556c73b'


On the bright side, it works exactly the same way without hiera_include()/modules/etc.

$ cat /tmp/file1
my contents

$ cat /tmp/t.pp
$dire = directory
$file= present
#$file = file

class myclass {
notice("dire=${::dire} and file=${::file}")
file { '/tmp/dir1':
ensure => $::dire,
}
file { '/tmp/file1':
ensure => $::file,
content => "file1 contents\n",
}
}

include ::myclass

$ puppet apply /tmp/t.pp
Notice: Scope(Class[Myclass]): dire=directory and file=present
Notice: Compiled catalog for cwl.hostopia.com in environment production in 0.07 seconds
Notice: /Stage[main]/Myclass/File[/tmp/file1]/content: content changed '{md5}579f6bb5beadcc02c12382fd65f583fb' to '{md5}a3814bc196b27f6b4c6409d2a0e5899f'
Notice: Finished catalog run in 0.15 seconds

$ cat /tmp/file1
file1 contents

jcbollinger

unread,
May 1, 2015, 8:58:37 AM5/1/15
to puppet...@googlegroups.com


On Thursday, April 30, 2015 at 1:00:45 PM UTC-5, Christopher Wood wrote:
I'm trying to pass ensure=> values in variables (and set resource defaults conditionally) and it doesn't seem to work that way. If there's some way of getting this working I am very happy to hear about it.

In my site.pp I have this sort of thing:

if $::devstyle and str2bool($::devstyle) {
  notify { 'et': message => 'devstyle on' }

  File {
    ensure => present,
  }
  $filefe = present
}
else {
  $filefe = file
}

The idea is that puppet will treat all file resources as ensure=>present anywhere that devstyle=true (it's an external fact). I do see the notify text in syslog when the agent run proceeds.

However, two things are not happening:

1) not getting the resource default when the resource does not have ensure=>
  (puppet reverts my manually modified file)

2) ensure => $::filefe is not ensuring 'present' when devstyle=true (special corporate bits censored out):

notify { 'mrt1':
  message => "filefe=${::filefe}"
}

file { '/etc/mrouted.conf':
  ensure  => $::filefe,
  content => "whatever\n",
}

The notify reports the expected value of $::filefe is as I expect, but the file's contents are still managed.



Yes.  You have specified that it should be so.

Evidently you have a misconception about the meaning of 'ensure => present'.  That does *not* mean "ignore all the other properties specified for this resource".  It means only "I don't care whether the target is a directory, symlink, or regular file, so long as it's present".  It is unusual to combine that with any value for 'content', as doing so implies that you *do* care what kind of filesystem object it is: it must be a regular file or a symlink pointing to a regular file.

Possibly you are looking for `replace => false`, instead.


John

Christopher Wood

unread,
May 1, 2015, 11:17:34 AM5/1/15
to puppet...@googlegroups.com
Yes, that was exactly it, thank you.


For posterity, it works with this in site.pp (3.7.2 master/agent):

if $::devstyle and str2bool($::devstyle) and $::cluster and 'dev' == $::cluster {
File {
replace => false,
}
notify { 'devstyle':
message => 'Not managing file contents, devstyle=true.',
}
}

And this in the inventory module:

if 'dev' == $::cluster {
$dscontents = "devstyle=true\n"
}
else {
$dscontents = "devstyle=false\n"
}

file { '/etc/facter/facts.d/devstyle.txt':
content => $dscontents,
}

Any developer wanting a real agent run on their pet host will remove that file and have a production-style agent run.

I will flatter myself by thinking that other people might also run into this. In that spirit I filed DOCUMENT-336 for a minor touchup.

https://tickets.puppetlabs.com/browse/DOCUMENT-336


Also for the next person to happen across this thread, there's a warning if the file is a symlink or something else while this behaviour applies:

$ ls -ld /tmp/this
lrwxrwxrwx 1 cwood cwood 4 May 1 10:48 /tmp/this -> that
$ cat /tmp/z.pp
file { '/tmp/this':
ensure => present,
content => "this file has a string in it\n",
# replace => false,
}
$ puppet apply /tmp/z.pp
Notice: Compiled catalog for cwl in environment production in 0.07 seconds
Warning: /Stage[main]/Main/File[/tmp/this]: Ensure set to :present but file type is link so no content will be synced
Notice: Finished catalog run in 0.10 seconds



>
> John
>
> --
> 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/4b6d2e84-5b00-4af6-90fa-86b2a4fcab6c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages