call exec into another class as a variable / how to get output of shell echo $(($(date +%s) / 60 / 60 / 24)) into variable

1,004 views
Skip to first unread message

jyotir bhandari

unread,
Feb 1, 2014, 11:15:37 AM2/1/14
to puppet...@googlegroups.com
Hi

I have date as shell command 'echo $(($(date +%s) / 60 / 60 / 24))', I want to call this command in parameter of puppet module, how can i do it.

or how can i use exec { "date": commnand => 'echo $(($(date +%s) / 60 / 60 / 24))' } redirect ouput into some variable as $date and call into puppet parameter.

Jyotir

José Luis Ledesma

unread,
Feb 1, 2014, 3:48:49 PM2/1/14
to puppet...@googlegroups.com

Hi,

  It seems you are thinking that puppet is like other scripting language, but it is not.   With puppet you define a final state, a model, so there is not "first I exec this, get the output and do something".

  If you ask how to get a date to model something, I would answer just use (custom) facts. But I think that the best question is why do you need that?

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/b1100867-b76c-4a5c-8663-96e03dbc04b8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

jyotir bhandari

unread,
Feb 2, 2014, 12:01:16 AM2/2/14
to puppet...@googlegroups.com
Hi


I tried to create custom facts, but still on client it is not reflecting as desired.

Please check the $::epoch_date called in setpass.

I'm trying setpass from http://projects.puppetlabs.com/projects/1/wiki/Password_Management_Patterns

Files

epoch_date.rb

require 'facter'
Facter.add(:epoch_date) do
  setcode do
    Facter::Util::Resolution.exec('echo $(($(date +%s) / 60 / 60 / 24))')
  end
end

[root@server ~]# facter --puppet epoch_date
16103

basic.pp

define accounts::basic($file, $key, $value, $delimiter = " ") {

    # passing the values via the environment simplifies quoting.
    Exec {
        environment => [ "P_KEY=$key",
                         "P_VALUE=$value",
                         "P_DELIM=$delimiter",
                         "P_FILE=$file" ],
        path => "/bin:/usr/bin",
    }

    # append line if "$key" not in "$file"
    exec { "append-$name":
        command => 'printf "%s\n" "$P_KEY$P_DELIM$P_VALUE" >> "$P_FILE"',
        unless  => 'grep -Pq -- "^\Q$P_KEY\E\s*\Q$P_DELIM\E" "$P_FILE"',
    }

    # update it if it already exists
    exec { "update-$name":
        command => 'perl -pi -e \'s{^\Q$ENV{P_KEY}\E\s*\Q$ENV{P_DELIM}\E.*}{$ENV{P_KEY}$ENV{P_DELIM}$ENV{P_VALUE}}g\' --  "$P_FILE"',
        unless  => 'grep -Pq -- "^\Q$P_KEY\E\s*\Q$P_DELIM\E\s*\Q$P_VALUE\E$" "$P_FILE"',
    }
}


setpass.pp


define accounts::setpass($file='/etc/shadow', $hash) {
  accounts::basic{ "set_pass_$name":
    file      => $file,
    key       => $name,
    value     => "$hash:$::epoch_date:0:99999:7:::",
    delimiter => ':'
    }
}


init.pp

class accounts {
 
   @accounts::system { 'demo':
       comment      => 'demo users',
   }
   @accounts::setpass { "demo":
        hash => '$6$salt$gUxP.t8.YpGkzSXlD5XOUBFAI7Jeat2pRraF7S2ycm36qprk76ihcTCqtgS3YS/dMk9j0EXIK..ZnVqi5YBKs1'
  }
}


Jyotir


















On Sunday, 2 February 2014 02:18:49 UTC+5:30, Jose Luis Ledesma wrote:

Hi,

  It seems you are thinking that puppet is like other scripting language, but it is not.   With puppet you define a final state, a model, so there is not "first I exec this, get the output and do something".

  If you ask how to get a date to model something, I would answer just use (custom) facts. But I think that the best question is why do you need that?

Regards,

El 01/02/2014 17:15, "jyotir bhandari" <jyotirb...@gmail.com> escribió:
--
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/b1100867-b76c-4a5c-8663-96e03dbc04b8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

José Luis Ledesma

unread,
Feb 2, 2014, 11:32:40 AM2/2/14
to puppet...@googlegroups.com

I don't understand why the epoch_date fact doesn't work,  why you cannot use the user resource? If it is because the password is enforced, you can use the user resource to create the user, and launch a chpasswd once created to setup the initial password.

Regards.

P.N. just realized the emails were sent just to me, please keep the group in copy.

El 02/02/2014 17:05, "jyotir bhandari" <jyotirb...@gmail.com> escribió:
Hi

I tried ${::epoch_date}, mistakenly i wrote with (). still it didnt work.

I have my master server on CentOS 5 and clients on Ubuntu 12.04

I have tried with the link of user resources, but it didnt work for me that is why i went with this approach.

Regards
Jyotir




On Sun, Feb 2, 2014 at 9:29 PM, José Luis Ledesma <joseluis...@gmail.com> wrote:

Hi,

First of all, which platform are you using? If it is linux, hpux, Solaris... you should use the 'user' resource:

http://docs.puppetlabs.com/references/latest/type.html

About using the epoch_date fact it is ${::epoch_date} note its {} and not ().

Regards,


El 02/02/2014 16:50, "jyotir bhandari" <jyotirb...@gmail.com> escribió:

Hi

Just tried as per your guidance $(::epoch_date), it didnt work, epoch_date field is not showing up in the shadow fields.

I tried to set password with libshadow too, but it didnt work for me, so i went with this approach, but now that hash is setting up, it is not setting epoch days, though i can hard code it but i want it to be correct as we can know when was the user actually created.'

Please guide me as to what is the best approach.


Regards
Jyotir




On Sun, Feb 2, 2014 at 1:35 PM, José Luis Ledesma <joseluis...@gmail.com> wrote:

I supose that user resource do not work for you, right? ( note that the link is an historic information, and perhaps  puppet currently supports your platform)

Have you tried ${::epoch_date}?

Regards,

El 02/02/2014 05:59, "jyotir bhandari" <jyotirb...@gmail.com> escribió:
Hi


Alan Castro

unread,
Feb 2, 2014, 11:54:00 AM2/2/14
to puppet...@googlegroups.com
Why don't you use inline_template and call a ruby function that returns what you want?

jyotir bhandari

unread,
Feb 2, 2014, 10:09:22 PM2/2/14
to puppet...@googlegroups.com
Hi Jose,


Thanks,  I was able to set it via chpasswd.


init.pp

class accounts {
   @accounts::system { 'demo':
       comment      => 'demo users',
       }
  exec { "demo":
          command => 'echo "demo:123456" | chpasswd',
          provider => 'shell',
       }
}

Thanks, again.

Jyotir



On Saturday, 1 February 2014 21:45:37 UTC+5:30, jyotir bhandari wrote:

jcbollinger

unread,
Feb 3, 2014, 10:19:32 AM2/3/14
to puppet...@googlegroups.com


On Sunday, February 2, 2014 10:54:00 AM UTC-6, Alan Castro wrote:
Why don't you use inline_template and call a ruby function that returns what you want?


Or use the generate() function, which would not require translating the command into Ruby (but might require wrapping it in "bash -c")?

Do note, however, that either of these two will run the code on the master, during catalog compilation, not on the target node, and not when the catalog is applied.


John

Reply all
Reply to author
Forward
0 new messages