如何在ansible中将列表的每个元素与字符串连接

Rao*_*aze 3 jinja2 ansible

我在 ansible var 中有一个字符串元素列表。我正在寻找如何使用定义的字符串附加到列表的每个元素。

你知道我该怎么做吗?我没有找到这样做的方法。

输入:

[ "a", "b", "c" ]
Run Code Online (Sandbox Code Playgroud)

输出:

[ "a-Z", "b-Z", "c-Z" ]
Run Code Online (Sandbox Code Playgroud)

Dou*_*oug 17

我真的不喜欢使用附加过滤器或循环。然而,我偶然发现这篇博客文章https://www.itix.fr/blog/ansible-add-prefix-suffix-to-list/,它使用了在 Ansible 2.9.x 中工作的不同方法。

- set_fact:
    output: "{{ list_to_suffix | product(['-Z']) | map('join') | list }}"
Run Code Online (Sandbox Code Playgroud)

  • 有用的提示 - 博客还展示了如何处理前缀大小写。 (4认同)

小智 7

下面是如何在一行中完成前缀和后缀

  - debug:
    var: result
  vars:
    prefix: foo1
    suffix: foo2
    a_list: [ "bar", "bat", "baz" ]
    result: "{{ [prefix] | product(a_list) | map('join') | list | product([suffix]) | map('join') | list }}"
Run Code Online (Sandbox Code Playgroud)


err*_*404 5

您可以join为此使用。请看下面的代码:

剧本-->

---
- hosts: localhost
  vars:
    input: [ "a", "b", "c" ]
  tasks:
    - name: debug
      set_fact:
        output: "{{ output | default([]) + ['-'.join((item,'Z'))] }}"
      loop: "{{ input | list}}"

    - debug:
        var: output
Run Code Online (Sandbox Code Playgroud)

输出 -->

PLAY [localhost] ********************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************
ok: [localhost]

TASK [debug] ************************************************************************************************************
ok: [localhost] => (item=a)
ok: [localhost] => (item=b)
ok: [localhost] => (item=c)

TASK [debug] ************************************************************************************************************
ok: [localhost] => {
    "output": [
        "a-Z",
        "b-Z",
        "c-Z"
    ]
}

PLAY RECAP **************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0
Run Code Online (Sandbox Code Playgroud)


Vla*_*tka 5

使用简单的过滤器

    shell> cat filter_plugins/string_filters.py
    def string_prefix(prefix, s):
        return prefix + s
    def string_postfix(postfix, s):
        return s + postfix
    class FilterModule(object):
        ''' Ansible filters. Python string operations.'''
        def filters(self):
            return {
                'string_prefix' : string_prefix,
                'string_postfix' : string_postfix
            }
Run Code Online (Sandbox Code Playgroud)

下面的任务

    - set_fact:
        output: "{{ input|map('string_prefix', '-Z')|list }}"
    - debug:
        var: output
Run Code Online (Sandbox Code Playgroud)

    "output": [
        "a-Z", 
        "b-Z", 
        "c-Z"
    ]
Run Code Online (Sandbox Code Playgroud)

相同的输出给出了下面的循环

    - set_fact:
        output: "{{ output|default([]) + [item + '-Z'] }}"
      loop: "{{ input }}"
    - debug:
        var: output
Run Code Online (Sandbox Code Playgroud)