I have the following params in hiera:
base::users:
[email protected]:
ensure: present
user: john
sudo: true
type: ssh-rsa
key: AAAAB3NzaC1yc2EAAAABJ
in puppet i'm getting the following hash:
{[email protected] => {ensure => present, user => john, sudo => true, type => ssh-rsa, key => AAAAB3NzaC1yc2EAAAABJ}}
Then i'm calling create resources to create appropriate authorized_keys file:
create_resources('ssh_authorized_key', $users)
but it doesn't work because i have added new parameter 'sudo' and before calling create_resources I want to remove this key from hash and operate in another resource.
I've tried the next step to remove it:
$users_filtered = $users.each |$k, $v| { $v.delete['sudo'] }
i'm getting the next error:
Error while evaluating a Function Call, delete(): Wrong number of arguments given 1 for 2.
As I understand puppet tried to use 'delete' function from stdlib module. But i have also tried:
$users_filtered = $users.each |$k, $v| { delete($users, $v['sudo'] }
But it doesn't work. Appreciate any help
Checking the documentation for the delete
function from stdlib
, we see that the two arguments in your case needs to be the hash to remove the key from and the key to remove from the hash.
https://github.com/puppetlabs/puppetlabs-stdlib#delete
$users_filtered = $users.each |$k, $v| { $v.delete['sudo'] }
The problem with this line is that you are treating delete
as a hash with a key sudo
. delete
is a function and not a hash. $v
is your hash values in the each
lambda iterator here. You can fix this with
$users_filtered = $users.each |$k, $v| { $v.delete('sudo') }
to treat delete
as a function. Also, if you want to pass $users_filtered
into a create_resources
function, then it needs to be a nested hash with each key as the title. Therefore, your lambda needs to be returning the nested hash, which means you need to use map
instead to return a nested hash.
$users_filtered = $users.map |$k, $v| { $v.delete('sudo') }
https://docs.puppet.com/puppet/4.10/function.html#map
Then we have the other attempt:
$users_filtered = $users.each |$k, $v| { delete($users, $v['sudo'] }
which also needs to be returning a hash and needs to have a key as the second argument. You are giving $v['sudo']
as the second argument, which is instead the value of the sudo
key in that hash. We fix this in a similar way via:
$users_filtered = $users.map |$k, $v| { delete($v, 'sudo'}
Note that the two versions of the solution are syntactically different but produce the same result and are both acceptable in modern Puppet DSL function invocations.
It is also worth noting that you can eliminate the need for the iterator entirely by using delete
on the entire hash from your example.
$users_filtered = delete($users, 'sudo')
Since Puppet 4.0.0, the minus (-) operator deletes values from arrays and deletes keys from a hash:
['a', 'b', 'c', 'b'] - 'b'
# would return ['a', 'c']
{'a'=>1,'b'=>2,'c'=>3} - ['b','c'])
# would return {'a' => '1'}
https://github.com/puppetlabs/puppetlabs-stdlib#delete
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With