简体   繁体   English

shell模块:<with ansible

[英]shell module: < with ansible

I want to run a command: 我想运行一个命令:

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

But achieve an error 但是实现了一个错误

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

I tried to change it to something like: 我试图将其更改为:

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

but it doesn't work. 但它不起作用。 Any thoughts? 有什么想法吗?

NB: this question about using < operator in shell module and I know that better to use apt for install something 注意:关于在shell模块中使用< operator的问题,我知道最好使用apt来安装一些东西

Use command module if you actually do not need shell . 如果您实际上不需要shell请使用command模块。

Also you will be better off using get_url module for downloading the file instead of relying on curl being installed on remote server. 此外,您最好使用get_url模块下载文件,而不是依赖于远程服务器上安装的curl Recent versions of Ansible will display a warning when you try to use curl instead of get_url module also: 当您尝试使用curl而不是get_url模块时,Ansible的最新版本将显示警告:

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

Here is how I would do this: 我是这样做的:

- 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

For extra options to get_url module visit: http://docs.ansible.com/get_url_module.html 有关get_url模块的额外选项,请访问: httpget_url

EDIT: though this answers this the question I think mgsk's answer is a better answer since I agree that it's not the right way to go about it with Ansible. 编辑:虽然这回答了这个问题我认为mgsk的答案是一个更好的答案,因为我同意这不是正确的方式来解决它与Ansible。

This should fix your issue: 这应该可以解决您的问题:

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

If you are wondering the difference between these two commands: 如果您想知道这两个命令之间的区别:

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

The first one uses process substitution which is a bash feature which is why you cannot use it with /bin/sh as your shell. 第一个使用进程替换 ,这是一个bash功能,这就是为什么你不能将它与/ bin / sh一起用作shell。 What it's doing is taking the output of the curl command (which is a python script), writing it to a temporary file and using that file as an argument to python which takes a python script as its first argument. 它正在做的是获取curl命令的输出(这是一个python脚本),将其写入临时文件并使用该文件作为python的参数,它将python脚本作为其第一个参数。

The second one is an ambiguous redirect because the python script that is generated from the curl is not a file 第二个是模糊重定向,因为从curl生成的python脚本不是文件

  - 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM