问题描述
我正在尝试设置一个剧本来部署 influxdb 集群.除了我们使用 INfluxDB Cluster Client
I'm trying to setup a playbook to deploy an influxdb cluster. Everything works fine except that we use INfluxDB Cluster Client
我正在生成一个由我们的脚本读取的 production.py 文件.
I am generating a production.py file which is read by our script.
InfluxdbClusterClient 正在等待一个元组列出所有涌入服务器:
InfluxdbClusterClient is waiting for a tuple to list all influx servers :
'hosts': [('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2', 8086), ('fr-influxdb-prod-3', 8086)],
我正在尝试从 ansible 变量生成这些元组,但我不知道如何处理.我试过了:
I am trying to generate those tuples from ansible variables but I don't know how to to dat. I tried:
/ Variables /
influxdb_hosts: [('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2', 8086), ('fr-influxdb-prod-3', 8086)]
/ Template /
....
'hosts': {{influxdb_hosts}},
...
不幸的是,它没有按预期工作,因为 Ansible 正在转换列表中的元组(即 [('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2',8086), ('fr-influxdb-prod-3', 8086)]
变成 ["('fr-influxdb-prod-1'", "8086)", "('fr-influxdb-prod-2'"," 8086)"," ('fr-influxdb-prod-3'"," 8086)]"
Unfortunately it doesn't work as expected as Ansible is transforming the tuple in list (ie [('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2', 8086), ('fr-influxdb-prod-3', 8086)]
becomes ["('fr-influxdb-prod-1'", "8086)", "('fr-influxdb-prod-2'"," 8086)"," ('fr-influxdb-prod-3'"," 8086)]"
你知道我做错了什么吗?
Do you see what i'm doing wrong ?
推荐答案
你可以使用折叠块标量来通知 Ansible 参数是一个不应被解析的字符串:
You can use folded block scalar to inform Ansible that the argument is a string which shouldn't be parsed:
influxdb_hosts: >
[('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2', 8086), ('fr-influxdb-prod-3', 8086)],
这是生成的目标文件:
'hosts': [('fr-influxdb-prod-1', 8086), ('fr-influxdb-prod-2', 8086), ('fr-influxdb-prod-3', 8086)],
这篇关于在ansible模板中生成元组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!