2

My requirement is to run the script stop-all as many times (5 retries) until the output of ps -fu user1 |wc -l becomes less than 2.

I wrote the below ansible playbook for the same:

cat stop.yml

  - hosts: dest_nodes
    tasks:
      - name: Start service
        include_tasks: "{{ playbook-dir }}/inner.yml"
        retries: 5
        delay: 4
        until: stopprocesscount.stdout is version('2', '<')


cat inner.yml

      - name: Start service
          shell: ~/stop-all
          register: stopprocess

      - name: Start service
          shell: ps -fu user1 |wc -l
          register: stopprocesscount

However, I get the below error running the playbook.

ERROR! 'retries' is not a valid attribute for a TaskInclude

The error appears to be in '/app/playbook/stop.yml': line 19, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


      - name: Start service
        ^ here

Can you please suggest?

Ashar
  • 2,942
  • 10
  • 58
  • 122

2 Answers2

2

First of all, correct the indentation of tasks in inner.yml. Secondly, remove retries, delay and until from stop.yml and move them to specific task as these are task level parameters.

Since you need to retry one task based on another task, you can just combine the script and command and extract the result of wc -l command like below:

Since stdout_lines will contain list of strings and version requires int hence the conversion.

inner.yml

  - name: Start service
    shell: ~/stop-all; ps -fu user1 | wc -l
    register: stopprocesscount
    retries: 5
    delay: 4
    until: stopprocesscount.stdout_lines[stopprocesscount.stdout_lines | length - 1] | int  is version('2', '<')

stop.yml

  - hosts: dest_nodes
    tasks:
      - name: Start service
        include_tasks: "{{ playbook-dir }}/inner.yml"
Moon
  • 2,837
  • 1
  • 20
  • 34
0

Not all task attributes work with all task (here TaskInclude task).

There is no clear documentation, such a compatibility matrix, but the error message here is quite clear "is not a valid attribute".

By instance:

You cant loop a Block: 'with_items' is not a valid attribute for a Block

You cant async a TaskInclude, see https://github.com/ansible/ansible/issues/57051.

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124