连接变量字符串的正确方法 [英] Proper way to concatenate variable strings
本文介绍了连接变量字符串的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从其他变量的内容创建新变量.目前我正在使用这样的东西:
I need to create new variable from contents of other variables. Currently I'm using something like this:
- command: echo "{{ var1 }}-{{ var2 }}-{{ var3 }}"
register: newvar
问题是:
- 使用
{{ var1 }}...{{ varN }}
会带来太长的字符串和非常难看的代码. {{ newvar.stdout }}
的使用要好一些,但很混乱.set_fact
模块的使用在运行之间缓存 fact.不适合我.
- Usage of
{{ var1 }}...{{ varN }}
brings too long strings and very ugly code. - Usage of
{{ newvar.stdout }}
a bit better but confusing. - Usage of
set_fact
module caches fact between runs. It isn't appropriate for me.
还有其他解决办法吗?
推荐答案
好问题.但我认为没有符合您标准的好答案.我能想到的最好的方法是使用额外的 vars 文件.
Good question. But I think there is no good answer which fits your criteria. The best I can think of is to use an extra vars file.
这样的任务:
- include_vars: concat.yml
在 concat.yml
中你有你的定义:
And in concat.yml
you have your definition:
newvar: "{{ var1 }}-{{ var2 }}-{{ var3 }}"
这篇关于连接变量字符串的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文