r/ansible•
-blind

Conditionally run shell script

Hi guys, I'm trying to learn Ansible and have been building out my own workstation playbook for provisioning them just as an exercise (mostly a forked variation of this).

But for the life of me I cannot figure out how to not run a script conditionally.

zPlug is dumb and is installed via a curl | zsh, I know - security risk but I've looked at it and w/e. How can I only run the curl | zsh if command -v zplug fails?

Things I've tried:

shell: if [[ command -v zplug ]]; then `curl | zsh`; fi 

shell: command -v zplug || curl | zsh

- name: Check for zplug
  shell: command -v zplug
  register: zplug_exists

- name: Install zPlug for ZSH
  shell: curl | zsh
  when: "zplug_exists.rc == 1"

And I'm out of ideas, help me please

Posted Jan 27, 2017, 5:04 AM UTC · edited

23
MiteshShah•
    - name: Check for zplug
  shell: if [ -d ~/.zplug ]; then echo 'yes'; else echo 'no'; fi
  ignore_errors: true
  register: zplug_installed

- name: Install zplug
  shell: curl | zsh
  ignore_errors: true
  when: zplug_installed.stdout.find("no") != -1
1
zufallsheld•

I tried this and found several problems:

  • The binary zplug did not exist after installing so the first task always failed.
  • you should use ignore_errors: true in you first task so it doesn't fail there

Like this:

  tasks:
  - name: Check for zplug
    shell: command -v zplug
    ignore_errors: true
    register: zplug_exists

Maybe just use the installation method with git?

- name: install zplug
  hosts: all
  tasks:
    - name: install zplug
      git: repo=https://github.com/zplug/zplug dest=/root/.zplug
1
[deleted]•

[removed]

1