Import Puppet environments from command line

Hi there,

I've successfully managed to set up a Foreman host, a Puppet control
repository and r10k on the Foreman host to pull changes from the repo
whenever they are made (and merged into the main branch). Hence, Puppet
environments are deployed automatically to the host. This is done by r10k,
triggered by a webhook, and r10k also has a postrun hook configured that
is supposed to tell Foreman that changes were just pulled and integrated.

What's still missing is to automatic updating / importing of Puppet
environments in Foreman. Instead of going to Configure > Environments
and klicking on the "Import environments from $hostname" button in the
Foreman UI I want to have this happen automatically whenever changes are
deployed by r10k – that's what the postrun hook is good for.

The command I run in the hook is:

/usr/sbin/foreman-rake puppet:import:puppet_classes[batch]

According to the Foreman source code this task updates both all
environments and classes.

Interestingly, despite some output on the console there is no change
visible in The Foreman. The list of environments remains unchanged (one
"production" entry only, just as originally installed). The output of the
command is as follows:

$ sudo /usr/sbin/foreman-rake puppet:import:puppet_classes[batch]'
Successfully encrypted field for Setting::Auth oauth_consumer_key
Successfully decrypted field for Setting::Auth oauth_consumer_key
Successfully decrypted field for Setting::Auth oauth_consumer_key
Successfully encrypted field for Setting::Auth oauth_consumer_secret
Successfully decrypted field for Setting::Auth oauth_consumer_secret
Successfully decrypted field for Setting::Auth oauth_consumer_secret

Any idea what's wrong? Why doesn't this command result in and update in The
Foreman? Or is there a more suitable command?

Thanks for any hints,
Peter

To answer my own question …

/usr/sbin/foreman-rake puppet:import:puppet_classes[false,master]

… seems to work. The two parameters are batch and envname. The task
imports the environment (= Git branch) "master" in non-batch mode (i.e. a
blocking call).

Note that I'm not a Ruby developer, I'm basically guessing. Neither do I
know Foreman well. Just reading and interpreting the code at

  1. Any Foreman wizard here to confirm my findings?
  2. Is there a way to parametrize the call in
    /etc/puppetlabs/r10k/r10k.yaml? (The webhook I'm calling to run r10k is
    passed the "master" parameter already, for example. Can I pass this on to
    the call somehow?)

Peter

··· Am Donnerstag, 1. Juni 2017 11:46:31 UTC+2 schrieb Peter Bittner: > > Hi there, > > I've successfully managed to set up a Foreman host, a Puppet control > repository and r10k on the Foreman host to pull changes from the repo > whenever they are made (and merged into the main branch). Hence, Puppet > environments are deployed automatically to the host. This is done by r10k, > triggered by a webhook, and r10k also has a `postrun` hook configured that > is supposed to tell Foreman that changes were just pulled and integrated. > > What's still missing is to automatic updating / importing of Puppet > environments in Foreman. Instead of going to *Configure* > *Environments* > and klicking on the "Import environments from $hostname" button in the > Foreman UI I want to have this happen automatically whenever changes are > deployed by r10k -- that's what the `postrun` hook is good for. > > The command I run in the hook is: > > /usr/sbin/foreman-rake puppet:import:puppet_classes[batch] > > According to the Foreman source code this task updates both all > environments and classes. > https://github.com/theforeman/foreman/blob/develop/lib/tasks/puppet.rake#L45-L46 > > Interestingly, despite some output on the console there is no change > visible in The Foreman. The list of environments remains unchanged (one > "production" entry only, just as originally installed). The output of the > command is as follows: > > $ sudo /usr/sbin/foreman-rake puppet:import:puppet_classes[batch]' > Successfully encrypted field for Setting::Auth oauth_consumer_key > Successfully decrypted field for Setting::Auth oauth_consumer_key > Successfully decrypted field for Setting::Auth oauth_consumer_key > Successfully encrypted field for Setting::Auth oauth_consumer_secret > Successfully decrypted field for Setting::Auth oauth_consumer_secret > Successfully decrypted field for Setting::Auth oauth_consumer_secret > > Any idea what's wrong? Why doesn't this command result in and update in > The Foreman? Or is there a more suitable command? > > Thanks for any hints, > Peter >

I cannot answer your question, but i can add one :stuck_out_tongue:
what is the difference between your solution and the following command:

hammer --verbose proxy import-classes --id 1

regards

1 Like

@bmx0r from the code it looks almost identical. Both call the same import function in the end. The hammer (CLI/API) version has more switches/filters (see below) to control the process the rake task seems to be more verbose. Also the hammer version can be called from remote client while rake can be run locally only.

$ hammer proxy import-classes -h
Usage:
	hammer proxy import-classes [OPTIONS]

Options:
 --dryrun                                Do not run the import
 --environment ENVIRONMENT_NAME          Environment name
 --environment-id ENVIRONMENT_ID
 --except EXCEPT                         Optional comma-delimited string containing either ‘new’, ‘updated’, or ‘obsolete’ that is used to limit the imported Puppet classes
 --id ID
 --location LOCATION_NAME                Location name
 --location-id LOCATION_ID
 --location-title LOCATION_TITLE         Location title
 --name NAME                             Name to search by
 --organization ORGANIZATION_NAME        Organization name
 --organization-id ORGANIZATION_ID       Organization ID
 --organization-title ORGANIZATION_TITLE Organization title
 -h, --help                              Print help

Regards,
Martin

1 Like

thanks Martin :slight_smile: