2016-09-17 98 views
1

我無法在Ansible的regex_replace過濾器中找到很多文字(點)。這裏是任務:使用'regex_replace'匹配字面值Ansible filter

- name: Display database name 
    debug: 
    msg: "{{ vhost | regex_replace('(.+\.)(.+)$', \\1) }}" 
    tags: [debug] 

我的目的是匹配並與它的第一部分(測試中的例子)更換整個URL像test.staging.domain.com

Ansible會報告以下錯誤:

debug: 
    msg: "{{ vhost | regex_replace('(.+\.)(.+)$', \\1) }}" 
            ^here 

We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value.

我怎樣才能匹配Ansible regexp_replace過濾文字?

回答

1

有可能是有關轉義字符一些怪誕的,但有一個逃生更低的方式來編寫一個字面點:

[.] 

所以,你的正則表達式可以寫成

(.+[.])(.+)$ 

大多數字符失去了在字符類中的特殊含義,而點是其中之一。

2

它實際上可能與雙側隙逃脫文字:

- name: Create database name and username 
    set_fact: 
    db_name: "{{ vhost | regex_replace('([^\\.]*)\\.(.+)$', '\\1') }}_stg" 

的正則表達式以上正常工作。第一個捕獲組提取URL的第一部分直到第一個點,整個正則表達式捕獲整個URL。通過test.staging.domain.co通過它會產生只是測試

1

我試圖做同樣的事情,最後做這樣的:

- name: Set hostname (minus the domain) 
    debug: msg={{ inventory_hostname | regex_replace('^([^.]*).*', '\\1') }} 

*編輯,發現了一個更好的方式:

- name: Set hostname (minus the domain) 
    debug: msg={{ inventory_hostname.split('.')[0] }}