How to loop over result and retry in the same task?

63 views
Skip to first unread message

Eric Chong

unread,
Jan 21, 2018, 10:03:35 PM1/21/18
to Ansible Project
For example,  if I want a task to pull ec2_instance_facts until any instance is in running state, and retry if none is running.

---
- hosts: localhost
  gather_facts: no
  connection: local

  tasks:
  - name: get ec2_instance_facts
    ec2_instance_facts:
    register: my_ec2
    until: item.state.name == 'running'
    with_items: '{{ my_ec2.instances }}'

But I am getting error, because my_ec2 is not defined yet.

TASK [get ec2_instance_facts] **********************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'my_ec2' is undefined"}

Is it possible to loop over the result and retry the task in a single task?  If I have to make a second task to loop over the result, I can't go back to retry the first task.  How does Ansible handle retry of multiple tasks?

Thanks

Brian Coca

unread,
Feb 6, 2018, 12:31:16 PM2/6/18
to Ansible Project
you cannot loop over the results of your task that is generating those
results, you CAN loop over the results of one task in a second task.





--
----------
Brian Coca

Eric Chong

unread,
Feb 8, 2018, 9:35:51 AM2/8/18
to Ansible Project
But I want to retry the first task until expected result.  I don't see a way for Ansible to retry a bock of tasks.

Brian Coca

unread,
Feb 8, 2018, 2:51:00 PM2/8/18
to Ansible Project
you CAN retry the first task, use until condition, i dont see a block
of tasks ...





--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages