简体   繁体   中英

Remove value from hash Puppet

I have the following params in hiera:

base::users:
  john@example.com:
    ensure: present
    user: john
    sudo: true
    type: ssh-rsa
    key: AAAAB3NzaC1yc2EAAAABJ

in puppet i'm getting the following hash:

 {john@example.com => {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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM