【问题标题】:How to pipe commands using Ansible? e.g. curl -sL host.com | sudo bash -如何使用 Ansible 管道命令?例如curl -sL host.com |须藤重击 -
【发布时间】:2017-12-27 15:14:49
【问题描述】:

我想通过 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": []}

【问题讨论】:

标签: ansible pipe


【解决方案1】:

你可以:

- 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 运行。

【讨论】:

  • 感谢您明确指出如何在 shell 模块中设置 warn:。 Doc对此并不清楚。
【解决方案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

【讨论】:

    【解决方案3】:

    如果你只想删除警告信息,你可以使用 shell 模块 (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 
    

    对于其他版本或操作系统,你可以更改 repo "https://rep.nodesource.com/setup_10.x" 例如 "https://deb.nodesource .com/setup_10.x”,setup 和 install 命令符合 SO。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2023-03-31
      • 2012-12-27
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      相关资源
      最近更新 更多