循环变量并与字符串连接 [英] Loop over variable and concatenate with string

查看:19
本文介绍了循环变量并与字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 ansible 中有一个数组:

Suppose I have an array in ansible:

vars:
  loopme: ['somesing', 'someothersing']
  concatenateme: 'constant'

我如何使用变量 concatenateme 遍历列表并连接列表中的值?

How do i iterate over the list and concat value from list with the variable concatenateme?

所以我得到 somesingconstantsomeothersingconstant 并将结果放入任务中的一个字段?也许与jinja?

So I get somesingconstant and someothersingconstant and put the result into a field in the task? Perhaps with jinja?

推荐答案

您可以使用 mapregex_replace 过滤器应用于列表的每个元素并替换end ofstring" ($) 和您的常量:

You can use map to apply regex_replace filter to every element of your list and replace "end of string" ($) with your constant:

- hosts: localhost
  gather_facts: no
  vars:
    loopme: ['somesing', 'someothersing']
    concatenateme: 'constant'
  tasks:
    - debug:
        msg: "{{ loopme | map('regex_replace','$',concatenateme) | list }}"

这篇关于循环变量并与字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆