Think before you speak, read before you think.

python打乱列表顺序

主要用到了 random 的 shuffle 方法

一个 resolv 的列表,每一次执行都输出不同的顺序

#!/usr/bin/env python
import random

resolv_servers = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
random.shuffle(resolv_servers)
print resolv_servers

同样,ansible 的 jinja2 模板里,定义了 resolv_servers,在不同机器下发时输出不同的顺序,使多台服务器均衡。

{% for server in resolv_servers|shuffle %}
 nameserver {{ server }}
 {% endfor %}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *