我的大多数位置都有4个以上的DNS源,但有几个则更少。每个位置都有自己的dns4_ips
列表变量,如下所示:
dns4_ips:
- dns_A
- dns_B
- dns_C
- dns_C
Run Code Online (Sandbox Code Playgroud)
我的resolv.conf模板如下所示:
domain example.com
search example.com dom2.example.com dom3.example.com
{% for nameserver in (dns4_ips|shuffle(seed=inventory_hostname)) %}
nameserver {{nameserver}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
Jinja for
循环工作得很好,但是在我有大量名称服务器的情况下,我只希望列出shuffle()
返回的前3个。
我想到了这一点:
nameserver {{ (dns4_ips|shuffle(seed=inventory_hostname))[0] }}
nameserver {{ (dns4_ips|shuffle(seed=inventory_hostname))[1] }}
nameserver {{ (dns4_ips|shuffle(seed=inventory_hostname))[2] }}
Run Code Online (Sandbox Code Playgroud)
...但是在某些情况下,我只有一个或两个DNS服务器可用,因此它们会产生错误的行或错误,对吗?
是否存在使用for循环处理此问题的干净方法,还是我需要在其中包装三个名称服务器行{% if (dns4_ips|shuffle(seed=inventory_hostname))[1] is defined %}
?