在Ansible中,要对列表中的字符串进行操作,可以使用以下过滤器:
示例:
- name: Join list elements
debug:
msg: "{{ my_list | join(',') }}"
vars:
my_list:
- foo
- bar
- baz
输出结果为: "foo,bar,baz"
示例:
- name: Convert list elements to uppercase
debug:
msg: "{{ my_list | map('upper') | list }}"
vars:
my_list:
- foo
- bar
- baz
输出结果为:["FOO", "BAR", "BAZ"]
示例:
- name: Replace substring in list elements using regex
debug:
msg: "{{ my_list | map('regex_replace', '^foo', 'new') | list }}"
vars:
my_list:
- foo
- foobar
- baz
输出结果为:["new", "newbar", "baz"]
以上是Ansible中操作列表中的字符串的一些常见方法。