如何使用定界符分割Ansible中的值 [英] how to split value in Ansible with delimiter

查看:174
本文介绍了如何使用定界符分割Ansible中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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正则表达式过滤器,您可以在此处找到:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆