2014-10-31 160 views
0

「=」字符我有一個​​劇本是這樣的:如何逃避ansible劇本

- name: make a http request 
    shell: wget -O /dev/null http://my.site.com/some/url?with=args 

的主要問題是GET參數。有一個「=」字符,我不知道如何正確地逃脫。 如果我不逃避這一點,ansible認爲它是模塊的另一個參數,但是如果我逃避它,那麼它會使雙重逸出,因此我在shell中得到了「/ =」。

Ansible版本:1.7.1

在此先感謝!

+0

http://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-in-bash-how-do-we-know-it – vaso123 2014-10-31 14:10:07

+0

問題不在於bash轉義,問題是在轉義腳本里逃脫 – Kron 2014-10-31 14:23:56

回答

0

我無法通過wget的裸機重現此問題。我使用我的gravatar作爲帶args的URL。這是一個示例調用。

TASK: [shell wget -O /dev/null https://www.gravatar.com/avatar/cad260683598a6e41608b91a9b57099b?s=24] *** 

而這裏的調試輸出:

<hostname> REMOTE_MODULE command wget -O /dev/null https://www.gravatar.com/avatar/cad260683598a6e41608b91a9b57099b?s=24 #USE_SHELL 
... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 1089 (1.1K) [image/jpeg]\nSaving to: `/dev/null'\n\n  0K .              100% 1.93M=0.001s\n\n2014-10-31 16:52:00 (1.93 MB/s) - `/dev/null' saved [1089/1089]", "stdout": ""} 

一般來說,使用commandshelllineinfile是代碼氣味的一種形式。在這種情況下,這意味着使用the get_url modulethe uri modulewget一般意味着get_url,但是您正在丟棄輸出,因此您正在使用它,如curl,這意味着uri模塊更好。

這是我的劇本。我需要添加pip,因爲我的測試主機上沒有安裝httplib。

tasks: 
    - debug: var=hostvars 
    - pip: name=httplib2 
    sudo: yes 
    - uri: url=https://www.gravatar.com/avatar/cad260683598a6e41608b91a9b57099b?s=24 

您可以在詳細輸出中看到它正在進行正確的調用。

ok: [hostname] => {"accept_ranges": "bytes", "access_control_allow_origin": "*", "cache_control": "max-age=300", "changed": false, "content_disposition": "inline; filename=\"cad260683598a6e41608b91a9b57099b.jpeg\"", "content_length": "1089", "content_location": "https://www.gravatar.com/avatar/cad260683598a6e41608b91a9b57099b?s=24", "content_type": "image/jpeg", "date": "Fri, 31 Oct 2014 16:58:42 GMT", "expires": "Fri, 31 Oct 2014 17:03:42 GMT", "last_modified": "Fri, 04 Nov 2011 16:27:09 GMT", "link": "<https://www.gravatar.com/avatar/cad260683598a6e41608b91a9b57099b?s=24>; rel=\"canonical\"", "redirected": false, "server": "ECS (ams/49F2)", "source_age": "81", "status": 200, "via": "1.1 varnish", "x_cache": "HIT", "x_varnish": "3775458975 3775177699"} 
+0

謝謝你的回答。我發現這個問題,看起來像是1.7.1版本中的一個bug。 我之所以用wget使用shell,是因爲我試圖使用具有最小依賴性的命令。在你使用uri模塊的例子中,它依賴於httplib2,所以我更喜歡使用幾乎在所有系統上實際存在的wget。 – Kron 2014-11-01 12:58:43

1

幾乎沒有幫助,發現問題。

問題實際上在Ansible 1.7.1中。升級到1.7.2後,問題解決了。

感謝大家:)