我有3台服务器,我发现我想将文件分别发送到3台服务器。
# hosts
[serverA]
192.168.1.1
[serverB]
192.168.1.2
[serverC]
192.168.1.3
我希望发送的文件如下:
# serverA_files:
server = 192.168.1.1
other = 192.168.1.2, 192.168.1.3
# serverB_files:
server = 192.168.1.2
other = 192.168.1.1, 192.168.1.3
# serverC_files:
server = 192.168.1.3
other = 192.168.1.1, 192.168.1.2
我正在准备jinja2的模板:
# test.j2
server = {{inventory_hostname}}
other = ??? # But these two values, I don't know how to assign them,
答案 0 :(得分:0)
您需要遍历声明为inventory names
的所有ip,过滤掉当前ip,然后用逗号将其余的ip连接起来。
最简单的解决方法是使用json_query
filter。为了简化转义和可读性,我将查询声明为模板内的变量。
server={{ inventory_hostname }}
{% set query = "[?@!='" + inventory_hostname + "']" %}
others={{ groups['all'] | json_query(query) | join(', ') }}