ansible YAML正则表达式过滤器
我试图提取API响应的guid值,下面是响应的示例:ansible YAML正则表达式过滤器,ansible,yaml,Ansible,Yaml,我试图提取API响应的guid值,下面是响应的示例: "api_request.json": { "message": "\"Role name 'mgmt-ALERTPUBLISHER-uuid-placeholder' is not compliant. Use 'mgmt-ALERTPUBLISHER-b7d445b08a96e7f19ff0ff005686cddd', or do not use a name of the format <service name&
"api_request.json": {
"message": "\"Role name 'mgmt-ALERTPUBLISHER-uuid-placeholder' is not compliant. Use 'mgmt-ALERTPUBLISHER-b7d445b08a96e7f19ff0ff005686cddd', or do not use a name of the format <service name>-<roletype>-<arbitrary value>.\""
}
这是我的正则表达式:
<div>\p{Any}*?</div>|[0-9a-f]{32}
如何将正则表达式与YAML一起使用
我只找到了regex_replace和regex_escape,但没有找到纯regex匹配器
regex_替换应该可以做到这一点 这是代码。我基本上是在搜索'ALERTPUBLISHER-'字符串,并在其后面剪切32个字符的字符串,就在便笺上方,然后在输出中使用它。 我注意到你的API是json格式的,如果你有可能要求一个只包含uuid字符串的额外字段,那么它可能会很方便。这将比任何正则表达式都更加健壮
- hosts: localhost
gather_facts: False
vars:
message: "\"Role name 'mgmt-ALERTPUBLISHER-uuid-placeholder' is not compliant. Use 'mgmt-ALERTPUBLISHER-b7d445b08a96e7f19ff0ff005686cddd', or do not use a name of the format <service name>-<roletype>-<arbitrary value>.\""
pre_tasks:
- debug:
msg: "{{ message | regex_replace('^.*ALERTPUBLISHER-(?P<uuid>.{32}).*$', '\\g<uuid>') }}"
PLAY [localhost] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [debug] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "b7d445b08a96e7f19ff0ff005686cddd"
}
PLAY RECAP ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0