Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - only run a series of tasks if a precondition is met

I have a package I need to install from a remote URL as in:

- get-url: url=http://foo.com/foo.deb dest=/tmp

- command: dpkg --skip-same-version -i /tmp/foo.deb

- apt: update_cache=yes

- apt: pkg=foo state=present

I'd only like to run the first 3 if pkg=foo isn't already present. What's the best way to achieve this?

like image 608
Jamie McCrindle Avatar asked Dec 31 '13 13:12

Jamie McCrindle


1 Answers

You have to register a variable with the result, and then use when statement.

tasks:
  - shell: /usr/bin/foo
    register: result
    ignore_errors: True

  - debug: msg="it failed"
    when: result|failed
like image 74
magnetik Avatar answered Sep 27 '22 23:09

magnetik