Skip to content

Commit 0fd9465

Browse files
authored
Merge pull request #4 from tuenti/concat-based
Concat based
2 parents 918a87d + 7076a2f commit 0fd9465

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

Modulefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ description 'Module to manage .netrc files'
88
project_page 'https://github.com/saheba/puppet-netrc.git'
99

1010
## Add dependencies, if any:
11-
# dependency 'username/name', '>= 1.2.0'
11+
dependency 'puppetlabs-concat', '>= 7.0.0'

manifests/init.pp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,47 @@
1212
#
1313
# Sample Usage: netrc::foruser("netrc_myuser": user => 'myuser', machine_user_password_triples => [['myserver.localdomain','myuser','pw'],['mysecondserver.localdomain','myuser','pw2']])
1414
# you can also override the full path by using the `file_path` parameter.
15-
# [Remember: No empty lines between comments and class definition]
1615
class netrc {
17-
1816
}
1917

20-
define netrc::foruser(
21-
Enum["present", "absent"] $ensure = "present",
22-
Stdlib::Absolutepath $home_base_directory = "/home",
23-
String $user,
24-
String $filename = ".netrc",
25-
Stdlib::Absolutepath $file_path = "$home_base_directory/$user/$filename",
26-
Hash[String, Hash] $machine_login_password) {
27-
28-
file { $file_path:
29-
ensure => $ensure,
30-
content => epp('netrc/netrc.epp', {
31-
machine_login_password => $machine_login_password
32-
}),
33-
mode => '0600',
34-
owner => "$user"
18+
define netrc::usermachine (
19+
String $user,
20+
String $machine,
21+
String $login,
22+
Sensitive[String] $password,
23+
Optional[String] $group = $user,
24+
Optional[String] $filename = '.netrc',
25+
Optional[Stdlib::Absolutepath] $file_path = undef,
26+
) {
27+
$user_file = $user ? {
28+
'root' => "/root/${filename}",
29+
default => "/home/${user}/${filename}",
30+
}
31+
$real_file_path = $file_path ? {
32+
undef => $user_file,
33+
default => $file_path,
34+
}
35+
if !defined(Concat[$real_file_path]) {
36+
concat { $real_file_path:
37+
ensure => present,
38+
mode => '0600',
39+
owner => $user,
40+
group => $group,
41+
ensure_newline => true,
42+
}
43+
concat::fragment { "${real_file_path}-header":
44+
target => $real_file_path,
45+
content => '# File content managed by Puppet',
46+
}
47+
}
48+
concat::fragment { "${user}-${machine}-${login}":
49+
target => $real_file_path,
50+
content => Sensitive(epp('netrc/netrc.epp',
51+
{
52+
machine => $machine,
53+
login => $login,
54+
password => $password,
55+
}
56+
)),
3557
}
3658
}

templates/netrc.epp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
<% $machine_login_password.each |$machine, $value| { -%>
2-
machine <%= $machine %> login <%= $value['login'] %> password <%= $value['password'] %>
3-
<%- } -%>
1+
machine <%= $machine %> login <%= $login %> password <%= $password %>

0 commit comments

Comments
 (0)