Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove value from hash Puppet

Tags:

hash

puppet

hiera

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

like image 644
BigBoss Avatar asked Dec 18 '22 07:12

BigBoss


2 Answers

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')
like image 188
Matt Schuchard Avatar answered Feb 11 '23 05:02

Matt Schuchard


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

like image 25
c33s Avatar answered Feb 11 '23 06:02

c33s