shell模块:<with ansible

kha*_*iuk 4 shell ansible ansible-playbook

我想运行一个命令:

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

但是实现了一个错误

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
Run Code Online (Sandbox Code Playgroud)

我试图将其更改为:

python <$(curl https://bootstrap.pypa.io/get-pip.py)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.有什么想法吗?

注意:这个关于<在shell模块中使用运算符的问题,我知道最好apt用于安装一些东西

Mic*_*sek 9

command如果您实际上不需要,请使用模块shell.

此外,您最好使用get_url模块下载文件,而不是依赖于curl安装在远程服务器上.当您尝试使用curl而不是get_url模块时,Ansible的最新版本将显示警告:

"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
Run Code Online (Sandbox Code Playgroud)

有关get_url模块访问的其他选项,请访问:http://docs.ansible.com/get_url_module.html


jar*_*arv 4

编辑:虽然这回答了这个问题,但我认为 mgsk 的答案是一个更好的答案,因为我同意这不是使用 Ansible 解决这个问题的正确方法。

这应该可以解决您的问题:

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

如果您想知道这两个命令之间的区别:

python <(curl https://bootstrap.pypa.io/get-pip.py)
python <$(curl https://bootstrap.pypa.io/get-pip.py)
Run Code Online (Sandbox Code Playgroud)

第一个使用进程替换,这是一个 bash 功能,这就是为什么你不能将它与 /bin/sh 作为 shell 一起使用。它所做的就是获取curl命令的输出(这是一个python脚本),将其写入一个临时文件,并使用该文件作为python的参数,python将python脚本作为其第一个参数。

第二个是不明确的重定向,因为从curl生成的python脚本不是一个文件