2017-06-29 124 views
-1

我試圖提取API響應的GUID值,這裏是響應的示例:ansible YAML正則表達式過濾

"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_replaceregex_escape但不是純粹的正則表達式匹配。

http://docs.ansible.com/ansible/playbooks_filters.html#json-query-filter

回答

2

的regex_replace應該做的伎倆。

這是代碼。我基本上搜索'ALERTPUBLISHER-'字符串,然後剪切它後面的32個字符的字符串,keeping it in a named group(就在註釋的上方),然後在輸出中使用它。
我注意到你的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