Ansible task execution until it completes

Dear All,

Could you please help with the below query.

I have variable_data which contains 3 records. I want to keep run debug task continuously every 1 minute until percent_complete == 100 and then complete task execution.

Please help to achieve my requirement. Thank you in advance.

TASK [debug] ***************************************************
ok: [cluster1] => {
“variable_data.msg”: {
“num_records”: 3,
“records”: [
{
“percent_complete”: 20,
“state”: “in-progress”,
“volume”: “vol1”,
“vserver”: “svm1”
},
{
“percent_complete”: 50,
“state”: “in-progress”,
“volume”: “vol2”,
“vserver”: “svm1”
},
{
“percent_complete”: 80,
“state”: “in-progress”,
“volume”: “vol3”,
“vserver”: “svm2”
}
]
}
}

This is not possible, because percent_complete is only 20, 60, or 80.

100 is not there. So whatever this means, it won’t finish.

I have another task (which will run long time) top of it where based on that this percent_complete will be changed and that date stored in variable_data.

I want to run debug task every 1 min to check percent_complete and if records shows percent_complete is 100 and then exit the play.

The play (debug) should execute every 1min and percent_complete will get changed every time till 100.

When percent_complete is reached 100 for every record in the variable_data, the play should exit.

Please help to achieve. Thank You.

Have you tried something like following?

  • name: wait for a url to work
    ansible.builtin.get_url:
    url: “https://myservice/
    dest: “/tmp/”
    register: result
    retries: 10
    delay: 6
    until: result.rc == 0

SInce your record is a json you should be able to pull that off easily.

Cheers,

Given the list for testing

  variable_data:
    - {percent_complete: 100, volume: vol1, vserver: svm1}
    - {percent_complete: 100, volume: vol2, vserver: svm1}
    - {percent_complete: 100, volume: vol3, vserver: svm2}

a task will complete when all *percent_complete* == 100

  data_done: "{{ variable_data|
                 selectattr('percent_complete', 'ne', 100)|
                 length == 0 }}"

Without knowing the source of *variable_data* it's not possible to
tell you how such a task should look like. Generally, you should test
*data_done*, set delay, and how many times you want to retry

  until: data_done
  delay: 60
  retries: 999

See "Retrying a task until a condition is met*
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#retrying-a-task-until-a-condition-is-met

Moving forward you should briefly describe at least: the inventory,
the source of the items(tasks, processes, ...) to be completed, and
how you consolidate *variable_data*.

Make it "Minimal reproducible example". See
https://en.wikipedia.org/wiki/Minimal_reproducible_example

Hi Vladimir,

Per suggestion, i have added the mentioned variable but still something is missing and i got below error:

The conditional check ‘data_done’ failed. The error was: Unexpected templating type error occurred on ({{ variable_data| selectattr(‘percent_complete’, ‘ne’, 100) | length == 0 }}): object of type ‘generator’ has no len()

Could you please assist? Thank you

This playbook is for NetApp storage module. moule ‘na_ontap_volu****me’ using for moving volume data from one aggregate(pool) to another.
Once ‘Volume move’ task is executed, I need to keep checking the status of volume move until 100% completed ( percent_complete ) in the list.

Add the explicit conversion to *list*

  data_done: "{{ variable_data|
                 selectattr('percent_complete', 'ne', 100)|
                 list>length == 0 }}"

This has already been fixed. If you can update the latest version.

Hi Vladimir,

Thank you.

I will check in higher version and update the status how it works.

Hi Vladimir,

I have added |list and execute below task in ansible [core 2.11.12] but no luck. Not getting where is stopping.

Error:

TASK [set_fact] *********************************
fatal: [10.250.198.160]: FAILED! => {}

MSG:

The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe_proxy.AnsibleUnsafeText object’ has no attribute ‘percent_complete’

The error appears to be in ‘/home/u630850/facts/netapp_volume_move.yml’: line 48, column 19, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  • debug: var=variable_data
    ^ here

There appears to be both ‘k=v’ shorthand syntax and YAML in this task. Only one syntax may be used.

Playbook as previously mentioned:

Try the tasks below. Register the *variable_data* in the first task
and use it in the starting *until* condition of the second task. You
want to repeat it every 1 minute. Set *delay* 60 seconds and *retry*
for example 60 times (1 hour)

      - netapp.ontap.na_ontap_rest_cli:
          <<: *login
          hostname: "{{ inventory_hostname }}"
          command: 'volume/move?fields=percent-complete,state'
          params:
            volume: '*'
            vserver: '*'
          verb: 'GET'
        register: variable_data

      - netapp.ontap.na_ontap_rest_cli:
          <<: *login
          hostname: "{{ inventory_hostname }}"
          command: 'volume/move?fields=percent-complete,state'
          params:
            volume: '*'
            vserver: '*'
          verb: 'GET'
        register: variable_data
        until: variable_data.msg.records|
               selectattr('percent_complete', 'ne', 100)|
               list>length == 0
        delay: 60
        retry: 60

This way you are actually monitoring the volumes. But, Ansible can't
provide you with any intermediate data. The task is running on the
remote host and you'll see the results once the task completes and
returns the results to the controller.

Try to configure NetApp system monitors if you want to see any
progress . See
https://docs.netapp.com/us-en/cloudinsights/task_system_monitors.html#monitor-descriptions

HI Vladimir,

Thank you again.

I think that is what I am looking for but it is when running with verbosity ( -vvvv).

Could you please go through below mentioned -vvv output where i can see required output under MSG ( highlighted in blue):

Can we get output of only MSG instead of listing all other output so that i can seethe required output?

TASK [run ontap rest cli command for vol move resister variabele] ***************************************************************************************************************************
task path: /home/user1/facts/netapp_volume_move.yml:38
< removed some data >
“attempts”: 1,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (34 retries left).Result was: {
“attempts”: 2,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (33 retries left).Result was: {
“attempts”: 3,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (32 retries left).Result was: {
“attempts”: 4,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
changed: [Cluster1] => {
“attempts”: 5,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
}
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘done’, ‘percent_complete’: 100}], ‘num_records’: 3}
Read vars_file ‘/home/user1/facts/cred_file_Privileged_account.yml’

This is the list of the records *variable_data.msg.records*

I can't provide you with better answer without "Minimal reproducible
example". https://en.wikipedia.org/wiki/Minimal_reproducible_example

Hi Team,

Greetings!

Could you please go through below mentioned -vvv output where i can see required output under MSG ( highlighted in blue):

Can we get output of only MSG instead of listing all other output so that i can seethe required output?

Thank you for your time and help.

TASK [run ontap rest cli command for vol move resister variabele] ***************************************************************************************************************************
task path: /home/user1/facts/netapp_volume_move.yml:38
< removed some data >
“attempts”: 1,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (34 retries left).Result was: {
“attempts”: 2,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (33 retries left).Result was: {
“attempts”: 3,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
FAILED - RETRYING: run ontap rest cli command for vol move resister variabele (32 retries left).Result was: {
“attempts”: 4,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
},
“retries”: 36
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘healthy’, ‘percent_complete’: 98}], ‘num_records’: 3}
< removed some data >
changed: [Cluster1] => {
“attempts”: 5,
“changed”: true,
“invocation”: {
“module_args”: {
“body”: {},
“cert_filepath”: null,
“command”: “volume/move?fields=percent-complete,state”,
“feature_flags”: {},
“hostname”: “Cluster1”,
“http_port”: null,
“https”: true,
“key_filepath”: null,
“ontapi”: null,
“params”: {
“volume”: “",
“vserver”: "

},
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“use_rest”: “Always”,
“username”: “user1”,
“validate_certs”: false,
“verb”: “GET”
}
}
}

MSG:

{‘records’: [{‘vserver’: ‘svm1’, ‘volume’: ‘svm1_mp_v3’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone2’, ‘state’: ‘done’, ‘percent_complete’: 100}, {‘vserver’: ‘svm2’, ‘volume’: ‘user1_test_clone3’, ‘state’: ‘done’, ‘percent_complete’: 100}], ‘num_records’: 3}
Read vars_file ‘/home/user1/facts/cred_file_Privileged_account.yml’