Ansible wget然后执行脚本 => get_url等效

33

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

我一直在思考如何用“ansible方式”(使用get_url等)来代替下面的shell任务,以更好地完成目标:
- name: Install oh-my-zsh
  shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -
或者
- name: Install nodesource repo
  shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -
7个回答

41

这个方法对我有效:

- 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

4
“script” 模块将本地脚本传送到目标主机,然后执行它。而 “get_url” 则是下载到目标主机上。因此你需要使用“shell”或“command”,而不是“script”。 - Jeffrey Hulten
2
这并不是一个解决方案,因为 shell 模块仍在使用。因此它无法解决幂等性问题(始终更改)。这使得 Playbook 更长(两个任务而不是一个)。这也会创建一个文件,即使在 tmp 文件系统中(在这种情况下)。必须有 get_urluri 模块的功能来解决这个问题... 希望在未来能够实现... - Drew
2
同意Drew的观点,这不是一个解决方案。 - Karthik T
脚本必须被启动的事实意味着任务必须在每次迭代中运行,无论涉及的Ansible命令/模块如何。我认为这一点处理得非常细致。 - steppo

9
@RaviTezu的解决方案行不通,因为您希望执行的文件/脚本必须在执行play/role的机器上。
根据这里的文档:

路径中的本地脚本将被传输到远程节点,然后执行。

因此,一种下载文件并使用以下任务的方法如下:
- name: execute the script.sh
  script: /local/path/to/script.sh

或者你可以这样做:
- 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/

如果您上传自己的脚本,我建议您选择第一种方法。但考虑到脚本可能会更新,所以在您的情况下更适合使用第二种方法,这样每次执行时都可以使用最新的脚本。请注意不要删除HTML标记。

3
这个playbook是我想出来的。到目前为止,它是最接近习惯用法的解决方案,并且似乎是幂等的。
它将检查命令的存在性(在这种情况下,是starship),如果/当测试失败时,它将下载脚本。
---
- 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

2
考虑使用get_urluri模块,而不是运行curl

例如:

- 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

2

对我来说,以下语句起作用:

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

6
如何避免使用 shell,而是使用 Ansible 原生模块?这是问题所在。 - Konstantin Suvorov

0

也许这个基本的例子可以帮助你开始:

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

0
注意:"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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接