Skip to content

Commit

Permalink
Merge pull request #4 from tuenti/concat-based
Browse files Browse the repository at this point in the history
Concat based
  • Loading branch information
Wiston999 authored Sep 12, 2023
2 parents 918a87d + 7076a2f commit 0fd9465
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Modulefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ description 'Module to manage .netrc files'
project_page 'https://github.com/saheba/puppet-netrc.git'

## Add dependencies, if any:
# dependency 'username/name', '>= 1.2.0'
dependency 'puppetlabs-concat', '>= 7.0.0'
56 changes: 39 additions & 17 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,47 @@
#
# Sample Usage: netrc::foruser("netrc_myuser": user => 'myuser', machine_user_password_triples => [['myserver.localdomain','myuser','pw'],['mysecondserver.localdomain','myuser','pw2']])
# you can also override the full path by using the `file_path` parameter.
# [Remember: No empty lines between comments and class definition]
class netrc {

}

define netrc::foruser(
Enum["present", "absent"] $ensure = "present",
Stdlib::Absolutepath $home_base_directory = "/home",
String $user,
String $filename = ".netrc",
Stdlib::Absolutepath $file_path = "$home_base_directory/$user/$filename",
Hash[String, Hash] $machine_login_password) {

file { $file_path:
ensure => $ensure,
content => epp('netrc/netrc.epp', {
machine_login_password => $machine_login_password
}),
mode => '0600',
owner => "$user"
define netrc::usermachine (
String $user,
String $machine,
String $login,
Sensitive[String] $password,
Optional[String] $group = $user,
Optional[String] $filename = '.netrc',
Optional[Stdlib::Absolutepath] $file_path = undef,
) {
$user_file = $user ? {
'root' => "/root/${filename}",
default => "/home/${user}/${filename}",
}
$real_file_path = $file_path ? {
undef => $user_file,
default => $file_path,
}
if !defined(Concat[$real_file_path]) {
concat { $real_file_path:
ensure => present,
mode => '0600',
owner => $user,
group => $group,
ensure_newline => true,
}
concat::fragment { "${real_file_path}-header":
target => $real_file_path,
content => '# File content managed by Puppet',
}
}
concat::fragment { "${user}-${machine}-${login}":
target => $real_file_path,
content => Sensitive(epp('netrc/netrc.epp',
{
machine => $machine,
login => $login,
password => $password,
}
)),
}
}
4 changes: 1 addition & 3 deletions templates/netrc.epp
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<% $machine_login_password.each |$machine, $value| { -%>
machine <%= $machine %> login <%= $value['login'] %> password <%= $value['password'] %>
<%- } -%>
machine <%= $machine %> login <%= $login %> password <%= $password %>

0 comments on commit 0fd9465

Please sign in to comment.