0

I have problem with conditions in ansible.
Here is my code sample:

// some code that generates result with lines
when: result.stdout_lines | length > 0
   block:
   - name: generates json
      // some magic
      register: jsonFile

   - name: processing json
     // some json parcing magic
     when condition in json 

The problem is: block is using for all sub-tasks condition when: result.stdout_lines | length > 0
If sub-task has its own condition, when: result.stdout_lines | length > 0 is dropped.
It is mentioned in documentation for block

So when ansible hits when: result.stdout_lines | length > 0 it is trying to do - name: processing json.
- name: processing json fails, because - name: generates json wasn't executed.

But i don't want to execute this 2 tasks if result is empty.
What should i use instead of block to "hide" sub-tasks from execution?

Ansible version is 2.9

UNIm95
  • 242
  • 4
  • 14

1 Answers1

0

you could use a second yaml-file sub.yml with plays in it:

- name: generates json
  // some magic
  register: jsonFile
- name: processing json
  // some json parcing magic
  when condition in json

and then call with include from the parent playbook:

  - name: i am the parent
    include: sub.yml
    when: result.stdout_lines | length > 0
Oliver Gaida
  • 1,722
  • 7
  • 14
  • also see [https://stackoverflow.com/search?q=%5Bansible%5D+include+playbook](https://stackoverflow.com/search?q=%5Bansible%5D+include+playbook) – Oliver Gaida Feb 20 '20 at 16:17
  • For my coded parts it will be to difficult to "extract" it in separate files. – UNIm95 Feb 20 '20 at 21:20
  • I have also tested your proposition. It has same result. It fails cause of non initialized json. – UNIm95 Feb 21 '20 at 11:09