Proper way to concatenate variable strings

后端 未结 3 921
夕颜
夕颜 2021-02-01 01:36

I need to create new variable from contents of other variables. Currently I\'m using something like this:

- command: echo \"{{ var1 }}-{{ var2 }}-{{ var3 }}\"
           


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 02:05

    Since strings are lists of characters in Python, we can concatenate strings the same way we concatenate lists (with the + sign):

    {{ var1 + '-' + var2 + '-' + var3 }}
    

    If you want to pipe the resulting string to some filter, make sure you enclose the bits in parentheses:

    e.g. To concatenate our 3 vars, and get a sha512 hash:

    {{ (var1 + var2 + var3) | hash('sha512') }}
    

    Note: this works on Ansible 2.3. I haven't tested it on earlier versions.

提交回复
热议问题