本文介绍了在Ansible模板中生成元组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个剧本来部署一个influxdb集群.除了使用 INfluxDB群集客户端

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正在等待元组列出所有Influx服务器:

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-3'-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)]"

您看到我在做什么吗?

推荐答案

您可以使用折叠块标量来通知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模板中生成元组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 07:57