如何使用分隔符在 Ansible 中分割值 [英] how to split value in Ansible with delimiter
问题描述
我正在 Ansible 中设置一个事实,并且该变量的值带有连字符,例如dos-e1-south-209334567829102380".我想拆分,所以我只能得到 "dos-e1-south"
I am setting a fact in Ansible and that variable has a value with hyphens, like this "dos-e1-south-209334567829102380". i want to split , so i only get "dos-e1-south"
这是剧本
- set_fact:
config: "{{ asg.results|json_query('[*].launch_configuration_name') }}"
- debug:
var: config
推荐答案
另一个选项是 ansibles 正则表达式过滤器,您可以在这里找到:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#regular-expression-filters
another option is ansibles regular expression filter, you find here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#regular-expression-filters
vars:
var: dos-e1-south-209334567829102380
tasks:
- debug:
msg: '{{ var | regex_replace("^(.*)-[^-]+$", "\\1") }}'
有相同的结果:
"msg": "dos-e1-south"
正则表达式说明:
^(.*)
在第一个反向引用中保留字符串开头的所有内容
keep everything from the start of the string in the first backreference
-[^-]+$
找到最后一个-"后跟非-"字符直到字符串结束.
find the last "-" followed by non-"-" characters till the end of string.
\\1
用第一个反向引用替换字符串.
replaces the string with the first backreference.
这篇关于如何使用分隔符在 Ansible 中分割值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!