Loop over variable and concatenate with string

2019-01-29 13:21发布

问题:

Suppose I have an array in ansible:

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

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

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

回答1:

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 }}"