onk*_*ows 2 ansible ansible-filter
特殊变量ansible_role_names包含一个在我的游戏中带有角色名称的数组。例如
"ansible_role_names": [
"geerlingguy.nfs",
"ANXS.openssh",
"robertdebock.bootstrap",
"robertdebock.squid"
]
Run Code Online (Sandbox Code Playgroud)
但是,我要访问的这个列表是删除点之前的所有内容的列表。
"roles": [
"nfs",
"openssh",
"bootstrap",
"squid" ]
Run Code Online (Sandbox Code Playgroud)
问:“使用 Ansible 时如何操作数组中的每一项?”
A: regex_replace过滤器是可能map的。例如戏剧
- set_fact:
my_list: "{{ ansible_role_names|
map('regex_replace', regex, replace)|
list }}"
vars:
regex: '^(.*)\.(.*)$'
replace: '\2'
- debug:
var: my_list
Run Code Online (Sandbox Code Playgroud)
给
"my_list": [
"nfs",
"openssh",
"bootstrap",
"squid"
]
Run Code Online (Sandbox Code Playgroud)
regex: '^(.*)\.(.*)$'
^ 匹配字符串的开头(.*) 匹配捕获组 No.1 中的任何字符\. 火柴 .(.*) 匹配第 2 号捕获组中的任何字符$ 匹配字符串的结尾replace: '\2'
\2 匹配先前定义的第 2 号捕获组| 归档时间: |
|
| 查看次数: |
996 次 |
| 最近记录: |