How to randomize the order of server names

97 views
Skip to first unread message

MK

unread,
Jul 23, 2017, 1:20:07 AM7/23/17
to Puppet Users
Does anyone have experiences randomizing the order of redundant server names  in the client configuration and won't change the order during next puppet agent runs?

For example, I have a bunch of ldap slave servers put in an array:

$_ldap_servers = ['ldap1' , 'ldap2', 'ldap3']

I want to randomize the order in the client config (ex, nslcd.conf or sssd.conf ) so they don't always hit the same ldap server.

ex:
ldap_search_base = dc=example, dc=com
ldap_uri = ldaps://ldap2 ldaps://ldap0 ldaps://ldap3

What I did so far is that in the ERB template, I put the following code:
````
ldap_uri = <%
       server_list = @_ldap_servers.shuffle
       server_list.size.times do
       server = server_list[rand(server_list.size)] -%>ldaps://<%= server -%>/ <%
         server_list.delete(server)
       end %>
```
It was working well with Rack based puppetmaster 3.x, but after upgrading to puppetserver 2.7.x, it changes the order every time when agent runs.

Does anyone have any suggestions?
Thanks so much!

MK

Rob Nelson

unread,
Jul 23, 2017, 10:51:17 AM7/23/17
to puppet...@googlegroups.com
You could try fqdn_rand(). Fqdn_rand will generate a random number, but the same random number for a given fqdn, so you would get the same value every time rather than it changing. https://docs.puppet.com/puppet/latest/function.html#fqdnrand

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/cfe4921b-d777-4b9e-8b6a-473f7c1904a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Rob Nelson

Martijn

unread,
Jul 24, 2017, 7:45:56 AM7/24/17
to Puppet Users
I think that's perfect for the OP's use case. And if you need multiple random numbers there's a useful second parameter to fqdn_rand(), the "seed", which can be any string or integer and allows you to get multiple different random numbers on the same host.

So, you could use the server_list.size as the seed to get a different but number after you pop off an item from your server_list array.
--
Martijn

Op zondag 23 juli 2017 16:51:17 UTC+2 schreef Rob Nelson:

Rob Nelson

unread,
Jul 24, 2017, 10:25:01 AM7/24/17
to puppet...@googlegroups.com
I did just notice this line, which should remove the .shuffle so it's consistent on every run:

       server_list = @_ldap_servers.shuffle

To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/63df98a6-ec85-4976-b300-0e3482857c99%40googlegroups.com.

Ramin K

unread,
Jul 24, 2017, 11:48:57 AM7/24/17
to puppet...@googlegroups.com
Forgot to reply on list, but we solved it with fqdn_rotate() doing most
of the work.

$_ldap_servers = ['ldap1','ldap2','ldap3']
$ldap_uri =
join(suffix(prefix(fqdn_rotate(sort($_ldap_servers))),'ldaps://'),'/'),' ')

Ramin

On 7/24/2017 7:24 AM, Rob Nelson wrote:
> I did just notice this line, which should remove the .shuffle so it's
> consistent on every run:
>
> server_list = @_ldap_servers.shuffle
>
>
>
> Rob Nelson
> rnel...@gmail.com <mailto:rnel...@gmail.com>
> <https://groups.google.com/d/msgid/puppet-users/cfe4921b-d777-4b9e-8b6a-473f7c1904a5%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> Rob Nelson
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/63df98a6-ec85-4976-b300-0e3482857c99%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/63df98a6-ec85-4976-b300-0e3482857c99%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CAC76iT9gQUG1m%3DK6-V%3DnOCNoVtd_cyGG%3DMjbgeMjKF31J5P6rA%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-users/CAC76iT9gQUG1m%3DK6-V%3DnOCNoVtd_cyGG%3DMjbgeMjKF31J5P6rA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages