community.general/lib/ansible
Dag Wieers 302eeae65d Do not register variable if host has been skipped
Executive summary: skipping a host corrupts a variable (when it is registered)

We have a play existing out of multiple tasks that check a condition, if one of these tasks fails we want to skip all next tasks in the playbook. I noticed that if we skip a task because a certain condition is met, and this task has a register-attribute, I loose the value in the variable. Which means we cannot use that variable in subsequent tasks to evaluate because it was skipped:

```
- action: command test -d /some/directory
  register: task

- action: command test -f /some/directory/file
  register: task
  only_if: '${task.rc} == 0'

- action: do something else
  only_if: '${task.rc} == 0'
```

In the above example, if the second task is skipped (because the first failed), the third action will end with a "SyntaxError: invalid syntax" complaining about the unsubstituted ${task.rc} (even though it was set by the first task and used for skipping the second).

The following play demonstrates the problem:

```
- name: Test register on ignored tasks
  hosts: all
  gather_facts: no

  vars:
    skip: true
    task: { 'rc': 666 }

  tasks:
  - action: debug msg='skip = ${skip}, task.rc = ${task.rc}'

  - name: Skip this task, just to test if task has changed
    action: command ls
    register: task
    only_if: '${skip} != True'

  - action: debug msg='skip = ${skip}, task.rc = ${task.rc}'

  - name: Now use task value
    action: command echo 'Works !'
    only_if: '${task.rc} == 0'
```

And the enclosed fix, fixes the above problem.
2012-11-13 16:57:10 +01:00
..
callback_plugins Abort a play at the start when no hosts matches, or no hosts are remaining 2012-10-02 15:54:24 +02:00
inventory Fix for an exception when for whatever reason the inventory script fails 2012-11-07 15:16:00 +01:00
playbook Do not register variable if host has been skipped 2012-11-13 16:57:10 +01:00
runner Merge pull request #1575 from dhozac/parameterized-playbook-include 2012-11-12 05:17:38 -08:00
utils Merge pull request #1575 from dhozac/parameterized-playbook-include 2012-11-12 05:17:38 -08:00
__init__.py Release version bump for 0.9 (development branch) 2012-10-19 21:01:57 -04:00
callbacks.py Make --oneline return one line (except with multiline output) 2012-11-08 14:49:40 +01:00
color.py Added a ANSIBLE_FORCE_COLOR env var, which forces the color output no matter what ncurses says. 2012-10-06 20:36:07 +02:00
constants.py Add ability to specify syslog facility for modules 2012-11-11 00:14:08 -08:00
errors.py Add 'polite' YAML syntax validation and hinting. Closes #798 2012-08-07 18:24:47 -04:00
module_common.py Add ability to specify syslog facility for modules 2012-11-11 00:14:08 -08:00