bash ansible wget 然后 exec 脚本 => get_url 等效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36965199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:35:24  来源:igfitidea点击:

ansible wget then exec scripts => get_url equivalent

bashcurlansiblewgetansible-playbook

提问by Oliboy50

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

我总是想知道shell使用“ansible 方式”(使用get_url等)替换以下任务的好方法是什么:

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

回答by RaviTezu

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

回答by sys0dm1n

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

@RaviTezu 解决方案不起作用,因为您希望执行的文件/脚本必须在您执行播放/角色的机器上。

As per the documentation here

根据此处的文档

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

path 处的本地脚本将传输到远程节点然后执行。

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.

如果您上传自己的脚本,我会选择第一种方法,第二种方法在您的情况下更有用,因为脚本可能会及时更新,因此您可以确定每次执行它时都会使用最新的脚本。

回答by Jan Clemens Stoffregen

For me, the following statement worked:

对我来说,以下语句有效:

 - name: "Execute Script"
   shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -

回答by kenorb

Consider using the get_urlor urimodule rather than running curl.

考虑使用get_urloruri模块而不是运行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

回答by ReSearchIT Eng

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.

请注意:“force=yes”,它将始终下载脚本,覆盖旧脚本。另请注意“changed_when”,您可以根据自己的情况对其进行优化。

  - 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

回答by Arbab Nazar

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