首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ansible中为列表的每个元素添加前缀

问在ansible中为列表的每个元素添加前缀
EN

Stack Overflow用户
提问于 2021-03-25 06:58:28
回答 1查看 34关注 0票数 1

我期待与netbios域名和反斜杠的用户列表。到目前为止,这是我能想到的最好的方法:

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

错误消息如下:

代码语言:javascript
复制
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认为我试图转义某些东西,但没有放入我想转义的东西。我也曾尝试使用双反斜杠,但无济于事。

有没有人能建议一下这里有什么可行的方法,或者另一种方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-25 07:38:45

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

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

可能会给你想要的

代码语言:javascript
复制
  mylist2:
  - test\Alice
  - test\Bob
  - test\Carol
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66790617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档