Ich frage mich immer, wie man die folgenden Shell
-Aufgaben auf die "ansible way" (mit get_url
usw.) ersetzt:
- name: Install oh-my-zsh
Shell: wget -qO - https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash -
oder
- name: Install nodesource repo
Shell: curl -sL https://deb.nodesource.com/setup_5.x | bash -
Das hat für mich funktioniert:
- 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
Die @RaviTezu-Lösung funktioniert nicht, da sich die Datei/das Skript, das Sie ausführen möchten, auf dem Computer befinden muss, auf dem Sie Ihr Spiel/Ihre Rolle ausführen.
Wie in der Dokumentation hier
Das lokale Skript unter Pfad wird an den Remote-Knoten übertragen und dann ausgeführt.
So können Sie dies tun, indem Sie die Datei lokal herunterladen und eine der folgenden Aufgaben verwenden:
- name: execute the script.sh
script: /local/path/to/script.sh
Oder Sie können das tun:
- 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/
Ich würde die erste Methode wählen, wenn Sie Ihr eigenes Skript hochladen. Die zweite Methode ist in Ihrem Fall nützlicher, da das Skript möglicherweise rechtzeitig aktualisiert wird. Sie sind also bei jeder Ausführung sicher, dass es das neueste Skript verwendet.
Für mich hat die folgende Aussage funktioniert:
- name: "Execute Script"
Shell: curl -sL https://rpm.nodesource.com/setup_6.x | bash -
Dieses grundlegende Beispiel kann Ihnen beim Start helfen:
---
- 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
Beachten Sie das: "force = yes", das immer das Skript herunterlädt und das alte überschreibt . Beachten Sie auch das "changed_when", das Sie in Ihrem Fall verfeinern können.
- 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
Erwägen Sie die Verwendung des Moduls
get_url
oderuri
anstattcurl
auszuführen.
Zum Beispiel:
- 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