在ansible中添加列表的每个元素

Prepend every element of a list in ansible

我希望在用户列表前加上 netbios 域名和反斜杠。到目前为止,这是我能想到的最好的:

- hosts: servers 
   vars:
    mylist: 
     - Alice
     - Bob 
     - Carol
    mystring: test
    mylist2: "{{ mylist | map('regex_replace', '^', mystring + '\' ) | list }}" 
  tasks:
   - debug: var=mylist2

错误信息如下:

The offending line appears to be:

mystring: test
mylist2: "{{ mylist | map('regex_replace', '^', mystring + '\' ) | list }}"
                                                            ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

with_items:
  - {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

我在将反斜杠连接到我的变量时遇到问题,因为 ansible 认为我正在尝试转义某些内容,但没有放入我想要转义的内容。我也尝试过使用双反斜杠无济于事。

有人可以建议在这里可行的方法或替代方法吗?

简化表达式,将regex_replace参数放入变量中,例如

- hosts: localhost
  vars:
    mylist: 
     - Alice
     - Bob 
     - Carol
    mystring: test
    mylist2: "{{ mylist|map('regex_replace', _my_regex, _my_replace)|list }}"
    _my_regex: '^(.*)$'
    _my_replace: '{{ mystring ~ "\" ~ "" }}'
  tasks:
    - debug:
        var: mylist2

大概给你想要的

  mylist2:
  - test\Alice
  - test\Bob
  - test\Carol