代码之家  ›  专栏  ›  技术社区  ›  Ivan

如何使用Ansible传递命令?e、 g.curl-sL主机。com | sudo bash-

  •  13
  • Ivan  · 技术社区  · 7 年前

    我想通过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 回复  |  直到 7 年前
        1
  •  26
  •   Konstantin Suvorov    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 跑。

        2
  •  4
  •   kenorb    7 年前

    考虑使用 get_url uri 模块而不是运行 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
  •  2
  •   Omar Alejandro Sotelo Torres    5 年前

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

      args:
        warn: no
    

    下面是shell属性命令,但忽略警告不是一个好做法,最好考虑使用get\u 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 
    

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