Puppet notes

From Wiki
Jump to: navigation, search


Start client in debug mode:

puppet agent --test --noop --server <server> --waitforcert 30 --environment test

Apply a catalog:

puppet apply --environment test /etc/puppet/environments/test/manifests/
cd /mnt/live/puppeteer/conf.d
puppet apply --debug --modulepath=puppet/modules -e '$puppeteer_cache="/mnt/live/puppeteer/conf.d"' ./

Remove environment from puppetdb:

\set env '''ipa_test'''
delete from factsets where environment_id = (select id from environments where environment = :env);
delete from catalogs where environment_id = (select id from environments where environment = :env);
delete from environments where environment = :env ;

Remove facts for a host:

delete from catalog_resources where certname_id = (select id from certnames where certname = 'host.example.com')

create random passwords:

$password = cache_data('mysql', 'mysql_password', random_password(32))

Create graph from dot files.

dot -Tpng relationships.dot -o relationships.png
dot -Tpng expanded_relationships.dot -o expanded_relationships.png
dot -Tpng resources.dot -o resources.png

Send a message to the client.

notify{"fqfn": message => "fqfn is: ${fqfn}"}
notify{"restart_events": message => "restart_events is: ${restart_events}"}

Lookup hiera values from the command line:

puppet apply -e '$somevar = hiera(foo) notify { $somevar: }'

Packages required in FreeBSD:

puppet puppetdb puppetdb-terminus git

Manifest Notes

Set up a bind mount.

mount { '/chroot/centos5/home': 
  ensure  => mounted, 
  device  => '/gpfs20/home', 
  fstype  => 'none', 
  options => 'rw,bind', 

Custom Facts

Custom facts can be added to puppet using facter and a bit of ruby code. For example, to add a fact named 'environment' create a file such as this.

Facter.add("environment") do
  setcode do
    Facter::Util::Resolution.exec('/bin/cat /usr/local/etc/environment')

The best place to put this would be in the "modules/puppet/lib/facter" directory.

Another option for defining custom facts is external facts. Place a text file in /etc/facter/facts.d using the format below.


Testing custom facts

Custom facts can be testing from the command line using the facter command.

cd ~/projects/puppet/modules/site/lib
export RUBYLIB=`pwd`
facter <fact_name>

The RUBYLIB variable causes facter to load any facts contained in the "facter" directory. You can also set a specific directory to use using the FACTERLIB variable.

cd ~/projects/facter
export FACTERLIB=`pwd`
facter <fact>


Array of hashes example:

  - Command: '/usr/sbin/backup-snapshot prepare'
    RunsWhen: Before
    RunsOnClient: yes
    RunsOnFailure: yes
    RunsOnSuccess: yes

  - Command: '/usr/sbin/backup-snapshot teardown'
    RunsWhen: After
    RunsOnClient: yes
    RunsOnFailure: yes
    RunsOnSuccess: yes

Puppet string substitution

Puppet has a regular expression substitution function built in.

$hostname_without_number = regsubst($hostname, '\d+$', )

Templates can also call out to ruby directly.

$hostname_without_number = inline_template('<%= hostname.gsub(/\d+$/, "") %>')