ansible wget then exec scripts => get_url equivalent
Solution 1
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
Solution 2
@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.
Solution 3
Consider using the
get_url
oruri
module rather than runningcurl
.
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
Solution 4
For me, the following statement worked:
- name: "Execute Script"
shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -
Solution 5
This playbook is what I've come up with. So far, it's as close to an idiomatic solution as I have come, and seems to be idempotent.
It will check for the existence of a command (in this case, starship
) and if/when the test fails it will download the script.
---
- name: Check for Starship command
command: command -v starship >/dev/null 2>&1
register: installed
no_log: true
ignore_errors: yes
- name: Download Starship installer
get_url:
url: https://starship.rs/install.sh
dest: /tmp/starship-installer.sh
mode: 'u+rwx'
when: installed.rc != 0
register: download
- name: Run the install script
shell: /tmp/starship-installer.sh
when: download.changed
- name: Remove the starship-installer.sh
file:
path: /tmp/starship-installer.sh
state: absent
Comments
-
Oliboy50 over 2 years
I always wonder what is the good way to replace the following
shell
tasks using the "ansible way" (withget_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 -
-
Konstantin Suvorov almost 7 yearsQuestion was how to avoid this and use Ansible native modules, not shell.
-
Jeffrey Hulten over 6 yearsThe 'script' module transfers a local script to the target host, then executes it. The 'get_url' downloads to the target host. Therefore you need 'shell' or 'command', not 'script'.
-
Drew about 6 yearsThis is not a solution as the shell module is still used. So it does not fix idempotency issue (always changed). and this makes the playbook longer (two tasks instead of one). This also creates a file albeit in tmp filesystem (in this case). There must be
get_url
oruri
modules functionality for this purposes... Hopefully in the future... -
Karthik T almost 6 yearsAgree with drew, this is not a solution.
-
steppo over 5 yearsThe fact that a script has to be launched implies that the task has to be run on every iteration, independently of the Ansible command/module involved. In my opinion this is carefully dealt with.