Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible wget then exec scripts => get_url equivalent

I always wonder what is the good way to replace the following shell tasks using the "ansible way" (with get_url, etc.):

- name: Install oh-my-zsh
  shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -

or

- name: Install nodesource repo
  shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -
like image 379
Oliboy50 Avatar asked May 01 '16 09:05

Oliboy50


3 Answers

This worked for me:

- name: Download zsh installer
  get_url: 
    url: https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh dest=/tmp/zsh-installer.sh
    
- name: Execute the zsh-installer.sh
  shell: /tmp/zsh-installer.sh

- name: Remove the zsh-installer.sh
  file: 
    path: /tmp/zsh-installer.sh 
    state: absent
like image 162
RaviTezu Avatar answered Oct 21 '22 05:10

RaviTezu


@RaviTezu solution doesn't work because the file/script that you wish to execute must be on the machine where you execute your play/role.

As per the documentation here

The local script at path will be transferred to the remote node and then executed.

So one way to do it is by downloading the file locally and using a task like below:

- name: execute the script.sh
  script: /local/path/to/script.sh

Or you can do this:

- name: download setup_5.x file to tmp dir
  get_url:
    url: https://deb.nodesource.com/setup_5.x
    dest: /tmp/
    mode: 0755

- name: execute setup_5.x script
  shell: setup_5.x
  args:
    chdir: /tmp/

I would go for the first method if you are uploading your own script, the second method is more useful in your case because the script might gets updated in time so you are sure each time you execute it it uses the latest script.

like image 24
sys0dm1n Avatar answered Oct 21 '22 05:10

sys0dm1n


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

For example:

- name: Download setup_8.x script
  get_url: url=https://deb.nodesource.com/setup_8.x dest=/opt mode=755
- name: Setup Node.js
  command: /opt/setup_8.x
- name: Install Node.js (JavaScript run-time environment)
  apt: name=nodejs state=present
like image 2
kenorb Avatar answered Oct 21 '22 03:10

kenorb