5

Example, I have 3 tasks, which are registered ec21, ec22 and ec23.

ec22 runs when ec21 fails, and ec23 runs when ec22 fails.

Now, I have another task below it which is :

- name: Add new instance to host group
  add_host:
    hostname: "{{ item.private_ip }}"
    groupname: launched
  with_items: "{{ ec2x.instances }}"

where ec2xis any of the previous tasks.

So, how do I set ec2x here? cause, Any of my 3 tasks can run. So, it can be ec21 or ec22 or ec23. So, how do I dynamically write that in the adding task after them?

5

It feels dirty, but I guess something like that would work:

- name: ec21
  [...]
  register: ec21_result

- set_fact: end_result= "{{ ec21_result }}"
  when: ec21_result|succeeded

- name: ec22
  [...]
  register: ec22_result
  when: ec21_result|failed

- set_fact: end_result= "{{ ec22_result }}"
  when: ec22_result|succeeded

- name: ec23
  [...]
  register: ec23_result
  when: ec22_result|failed

- set_fact: end_result= "{{ ec23_result }}"
  when: ec23_result|succeeded

- name: Add new instance to host group
  add_host:
    hostname: "{{ item.private_ip }}"
    groupname: launched
  with_items: "{{ end_result.instances }}"

Basically setting the end_result as soon as you succeed a play.

| improve this answer | |
  • Seems to work. But, this small issue is roadblocking the entire thing :D . Possible to know how to stop the failed set_facts tasks from setting empty values to it? – Dawny33 May 8 '17 at 12:20
  • You mean that if ec22 is ok, and then ec23 fail, then the fact will be set to an empty value ? What's your Ansible version ? – ArbustRe May 9 '17 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.