如何使用Ansible管道命令?例如curl -sL host.com | sudo bash -

时间:2017-12-27 15:14:50

标签: ansible pipe

我想通过Ansible发出命令:

curl -sL https://deb.nodesource.com/setup | sudo bash -

我怎么能通过Ansible做到这一点?现在我有:

- name: Add repository
  command: curl -sL https://deb.nodesource.com/setup | sudo bash -

但是它会引发错误:

[WARNING]: Consider using get_url or uri module rather than running curl

fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["curl", "-sL", "https://deb.nodesource.com/setup", "|", "sudo", "bash", "-"], "delta": "0:00:00.006202", "end": "2017-12-27 15:11:55.441754", "msg": "non-zero return code", "rc": 2, "start": "2017-12-27 15:11:55.435552", "stderr": "curl: option -: is unknown\ncurl: try 'curl --help' or 'curl --manual' for more information", "stderr_lines": ["curl: option -: is unknown", "curl: try 'curl --help' or 'curl --manual' for more information"], "stdout": "", "stdout_lines": []}

3 个答案:

答案 0 :(得分:7)

你可以:

- name: Add repository
  shell: curl -sL https://deb.nodesource.com/setup | sudo bash -
  args:
    warn: no

shell允许管道warn: no禁止警告。

但如果我是你,我会使用apt_key + apt_repository Ansible模块来创建也支持check_mode运行的自解释剧本。

答案 1 :(得分:2)

  

考虑使用get_urluri模块,而不是运行curl

例如:

- name: Download Node.js setup script
  get_url: url=https://deb.nodesource.com/setup dest=/opt mode=755
- name: Setup Node.js
  command: /opt/setup
- name: Install Node.js (JavaScript run-time environment)
  apt: name=nodejs state=present

答案 2 :(得分:0)

如果只想删除警告消息,则可以使用外壳模块(https://docs.ansible.com/ansible/latest/modules/shell_module.html#examples)并添加属性

  args:
    warn: no

在shell属性命令下面,但是忽略警告不是一个好习惯,最好考虑使用get_url模块(https://docs.ansible.com/ansible/latest/modules/get_url_module.html#examples),例如,对于Centos 7中的Node 10安装,可以使用:

   - name: Download NodeJs script
     get_url:
       url: https://rep.nodesource.com/setup_10.x 
       dest: /opt/nodesetup
       mode: 0755

   - name: Execute setup NodeJs script
     shell: /opt/nodesetup

   - name: Install NodeJs
     yum:
       name: nodejs
       state: present 

对于其他版本或操作系统,您可以更改存储库“ https://rep.nodesource.com/setup_10.x” ,例如“ https://deb.nodesource .com / setup_10.x ”,并且设置和安装命令与SO一致。