Variable change if condition is not met !!! Register "skipped" ?

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 gives the 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 ?

registering a variable isn’t meant to be skipped, it works as designed. If you want to build something like this you’ll have to register different variables, and then use set_vars to create the output one

Adrian Paraschiv adrian.paraschiv.mobile@gmail.com napisał:

Thanks for the fast response but I don’t get it and google is not helping me in finding an example or even how this module: “set_vars” works.
So you say that if I put something like this:

  • name: “play deps if ACE_*”
    shell: “cd /opt/play/apps/default-ace && play deps {{ dep }}”
    register: checkdeps1
    when: “dep == "1"”
  • name: “play deps example to skip”
    shell: “cd /opt/play/apps/default-ace && play deps {{ dep }}”
    register: checkdeps2
    when: “dep == "2"”
  • name: “play deps example to skip”
    shell: “cd /opt/play/apps/default-ace && play deps {{ dep }}”
    register: checkdeps100
    when: “dep == "100"”

Where do I put set_vars ???

Sorry, it’s set_fact, not set_vars. You’d add a task like this at the end:

  • set_fact:
    checkdeps: ‘{{ checkdeps1 if not (checkdeps1 | skipped) else checkdeps2 }}’

Or put something like this after each of the tasks registering checkdeps*:

  • set_fact:
    checkdeps: ‘{{ checkdeps1 }}’
    when: ‘{{ not (checkdeps1 | skipped) }}’

Adrian Paraschiv adrian.paraschiv.mobile@gmail.com napisał: