ansible wget then exec scripts => get_url equivalent
Asked Answered
P

7

34

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 -
Pontic answered 1/5, 2016 at 9:39 Comment(0)
E
42

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
Earphone answered 1/5, 2016 at 12:5 Comment(4)
The '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'.Succoth
This 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 or uri modules functionality for this purposes... Hopefully in the future...Gemini
Agree with drew, this is not a solution.Pearlinepearlman
The 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.Nuremberg
P
9

@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.

Progression answered 30/3, 2017 at 10:14 Comment(0)
E
4

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
Essequibo answered 30/4, 2021 at 1:6 Comment(0)
S
2

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
Sniffy answered 15/4, 2018 at 16:53 Comment(0)
S
1

For me, the following statement worked:

 - name: "Execute Script"
   shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -
Stubbed answered 30/6, 2017 at 11:11 Comment(2)
Question was how to avoid this and use Ansible native modules, not shell.Leaseback
Downvoted, because the question is precisely "how not to do that?"Tortuosity
O
0

May be this basic example can help you to start:

---
- name: Installing Zsh and git
  apt: pkg=zsh,git state=latest
  register: installation

- name: Backing up existing ~/.zshrc
  shell: if [ -f ~/.zshrc ]; then mv ~/.zshrc{,.orig}; fi
  when: installation|success
  sudo: no

- name: Cloning  oh-my-zsh
  git:
    repo=https://github.com/robbyrussell/oh-my-zsh
    dest=~/.oh-my-zsh
  when: installation|success
  register: cloning
  sudo: no

- name: Creating new ~/.zshrc
  copy:
    src=~/.oh-my-zsh/templates/zshrc.zsh-template
    dest=~/.zshrc
  when: cloning|success
  sudo: no
Offspring answered 1/5, 2016 at 14:31 Comment(0)
P
0

Note the: "force=yes", which will always download the script, overriding the old one. Also note the "changed_when", which you can refine per your case.

  - name: 'Download {{ helm.install_script_url }}'
    environment:
      http_proxy:  '{{proxy_env.http_proxy | default ("") }}'
      https_proxy: '{{proxy_env.https_proxy | default ("") }}'
      no_proxy:    '{{proxy_env.no_proxy | default ("") }}'
    get_url: url={{ helm.install_script_url | default ("") }} dest=/tmp/helm_install_script force=yes mode="0755"
    when: helm.install_script_url is defined
    tags:
    - helm_x

  - name: Run {{ helm.install_script_url }}
    environment:
      http_proxy:  '{{proxy_env.http_proxy | default ("") }}'
      https_proxy: '{{proxy_env.https_proxy | default ("") }}'
      no_proxy:    '{{proxy_env.no_proxy | default ("") }}'
    command: "/tmp/helm_install_script"
    register: command_result
    changed_when: "'is up-to-date' not in command_result.stdout"
    when: helm.install_script_url is defined
    args:
      chdir: /tmp/
    tags:
    - helm_x
Penman answered 21/4, 2017 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.