Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe commands using Ansible? e.g. curl -sL host.com | sudo bash -

Tags:

pipe

ansible

I want to make the command via Ansible:

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

How can I do it via Ansible? Now I have:

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

But it throw error:

[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": []}

like image 870
Ivan Avatar asked Dec 27 '17 15:12

Ivan


3 Answers

You can:

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

shell to allow pipes, warn: no to suppress warning.

But if I were you, I'd use apt_key + apt_repository Ansible modules to create self explaining playbook that also support check_mode runs.

like image 163
Konstantin Suvorov Avatar answered Nov 06 '22 12:11

Konstantin Suvorov


If you only wants to remove the warning message, you can use shell module (https://docs.ansible.com/ansible/latest/modules/shell_module.html#examples) and add the property

  args:
    warn: no

Below of shell property command, but it's not a good practice ignore warnings, it's better if you consider use the get_url module (https://docs.ansible.com/ansible/latest/modules/get_url_module.html#examples), for example for a Node 10 installation in a Centos 7, you can use:

   - 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 

For others versions or OS, yo can change the repo "https://rep.nodesource.com/setup_10.x" for example for "https://deb.nodesource.com/setup_10.x" and the setup and install commands accord with the SO.

like image 22
Omar Alejandro Sotelo Torres Avatar answered Nov 06 '22 10:11

Omar Alejandro Sotelo Torres


Consider using the get_url or uri module rather than running curl.

For example:

- 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
like image 27
kenorb Avatar answered Nov 06 '22 12:11

kenorb