ady8531
ady8531

Reputation: 678

Ansible - variable changing it's value even though the condition is not met

I have a role that I need to use for more values. For each task within the role I register a variable: checkdeps (it's the same for all tasks within this role - during a run it always has at least one value/output - I need it like this because the path differs "/opt/play/apps/default-ace", "default-device" etc.) and in the end I do an echo to view the output of checkdeps.stdout.

Below I've put one task that will output ok and one that will intentionally will be skipped. If I use the parameter dep: APK_PARSER in the playbook what it does is: first checkdeps registers the output and in the second task the value of checkdeps is replaced with nothing! Even though the task is skipped due to no matching dep parameter.

Why does the value of checkdeps is replaced if the condition is not met ?

- name: "output ok"
  shell: "cd /opt/play/apps/default-ace && play deps {{ dep }}"
  register: checkdeps
  when: "dep == \"APK_PARSER\""

- name: "example to skip"
  shell: "cd /opt/play/apps/default-device && play deps {{ dep }}"
  register: checkdeps
  when: "dep == \"I\" or dep == \"II\""

- name: "echo ok if Done!"
  shell: "echo \"OK - {{ dep }} Dependencies {{ checkdeps.stdout }}\""

And it gives me error:

One or more undefined variables: 'dict' object has no attribute 'stdout'

I've modified the last line without the stdout:

shell: "echo \"OK - {{ dep }} Dependencies {{ checkdeps }}\""

and it ran without error but gave the wrong output:

stdout:
OK - APK_PARSER Dependencies {u'skipped': True, u'changed': False}

did the variable checkdeps register the "skipping: [...]" ? Why it is changing it's value if the condition is not met ?

Upvotes: 3

Views: 2970

Answers (2)

boscowitch
boscowitch

Reputation: 41

Or simply tell ansible to not register the variable if the task is skipped with set_when_task_skipped=false:

- name: "example to skip"
  shell: "cd /opt/play/apps/default-device && play deps {{ dep }}"
  register: checkdeps set_when_task_skipped=false
  when: "dep == \"I\" or dep == \"II\""

Upvotes: 1

Kashyap
Kashyap

Reputation: 17544

ansible stores the "log of ansible task execution", NOT the 'output of command executed'. This log which is a dict and one of the keys is stdout, which contains everything the executed command printed on stdout (output of command).

  tasks:
    - debug: msg='one'
      register: o1
      when: True
    - debug: msg='two'
      register: o2
      when: False
    - debug: msg='o1={{o1}}'
    - debug: msg='o2={{o2}}'

It prints the following. 'skipped' & 'changed' are the two keys that would be present in the "log" when the task is not executed.

TASK: [debug msg='one'] ******************************************************* 
ok: [localhost] => {
    "msg": "one"
}

TASK: [debug msg='two'] ******************************************************* 
skipping: [localhost]

TASK: [debug msg='o1={{o1}}'] ************************************************* 
ok: [localhost] => {
    "msg": "o1={'msg': u'one', 'verbose_always': True, 'invocation': {'module_name': u'debug', 'module_args': u\"msg='one'\"}}"
}

TASK: [debug msg='o2={{o2}}'] ************************************************* 
ok: [localhost] => {
    "msg": "o2={u'skipped': True, u'changed': False}"
}

* term "task execution log" is invented by me for explanation and not ansible standard terminology.

Upvotes: 3

Related Questions