2014-12-03 121 views
4

我想運行一個命令:外殼模塊:<與ansible

- name: install pip 
    shell: "python <(curl https://bootstrap.pypa.io/get-pip.py)" 

但實現一個錯誤

failed: [default] => {"changed": true, "cmd": "python <(curl https://bootstrap.pypa.io/get-pip.py)", "delta": "0:00:00.002073", "end": "2014-12-03 15:52:01.780837", "rc": 2, "start": "2014-12-03 15:52:01.778764", "warnings": []} 
stderr: /bin/sh: 1: Syntax error: "(" unexpected 

我試圖改變它的東西,如:

python <$(curl https://bootstrap.pypa.io/get-pip.py) 

但它不起作用。有什麼想法嗎?

注:約在外殼模塊使用<運營商,這個問題我知道,最好使用apt用於安裝的東西

+2

'shell'幾乎可以肯定使用'/ bin/sh'運行命令。 '/ bin/sh'不支持進程替換。取而代之的是使用'bash',或者你可以嘗試用curl輸出到python。 – 2014-12-03 16:04:56

回答

3

編輯:雖然這回答了這個問題,我認爲mgsk的回答是,因爲一個更好的答案我同意這不是與Ansible合作的正確方法。

這應該可以解決您的問題:

- name: install pip 
    shell: "python <(curl https://bootstrap.pypa.io/get-pip.py)" executable=/bin/bash 

如果你想知道這兩個命令的區別是:

python <(curl https://bootstrap.pypa.io/get-pip.py) 
python <$(curl https://bootstrap.pypa.io/get-pip.py) 

第一個使用process substitution這是一個bash的功能這就是爲什麼你不能在/ bin/sh中使用它作爲你的shell。它所做的是將curl命令(它是一個python腳本)的輸出,將其寫入一個臨時文件,並將該文件用作python的參數,該參數將python腳本作爲其第一個參數。

第二個是一個模糊的重定向,因爲這是來自卷邊產生的Python腳本不是一個文件

9

使用command模塊,如果你其實並不需要shell

此外,您將使用get_url模塊更好地下載文件,而不是依賴curl安裝在遠程服務器上。

"warnings": ["Consider using get_url module rather than running curl"]

這裏是我會怎麼做:

- name: Download pip installer 
    get_url: 
    url=https://bootstrap.pypa.io/get-pip.py 
    dest=/tmp/get-pip.py 
    mode=0440 

- name: Install pip 
    command: /usr/bin/python /tmp/get-pip.py 

對於額外的選項來get_url當您嘗試使用curl代替get_url模塊還 最近Ansible的版本會顯示警告模塊訪問:http://docs.ansible.com/get_url_module.html

0
- name: configure zookeeper /etc/zookeeper/conf/zoo.cfg 
    shell: "{{ item }}" 
    with_items: 
     if [ -f /etc/zookeeper/conf/zoo_cfg.org ] ; 
     then cp /etc/zookeeper/conf/zoo_cfg.org /etc/zookeeper/conf/zoo.cfg ; 
     else cp /etc/zookeeper/conf/zoo.cfg /etc/zookeeper/conf/zoo_cfg.org; 
     fi; 
     cat /vagrant/zoo.cfg.j2 >> /etc/zookeeper/conf/zoo.cfg; 
+0

請解釋您的解決方案。 – 2016-02-27 20:46:13