2014-12-03 205 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