<p>It feels dirty, but I guess something like that would work:</p>

<pre><code>- 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 }}"
</code></pre>

<p>Basically setting the <code>end_result</code> as soon as you succeed a play.</p>

CC BY-SA 3.0
2017-05-08T12:20:52.110Z
Seems to work.  But, this [small issue](https://www.reddit.com/r/ansible/comments/69xhkw/why_is_my_set_fact_play_running_even_when_the/?st=j2g3fluo&sh=7c840e20) is roadblocking the entire thing :D . Possible to know how to stop the failed `set_facts` tasks from setting empty values to it?
Dawny33
CC BY-SA 3.0
2017-05-09T13:41:36.660Z
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
CC BY-SA 3.0
2017-05-05T09:48:21.497Z
2017-05-05T09:48:21.497Z
ArbustRe
<p>Example, I have 3 tasks, which are registered <code>ec21</code>, <code>ec22</code> and <code>ec23</code>.</p>

<p><code>ec22</code> runs when <code>ec21</code> fails, and <code>ec23</code> runs when <code>ec22</code> fails.</p>

<p>Now, I have another task below it which is :</p>

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

<p>where <code>ec2x</code>is any of the previous tasks. </p>

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

<ansible>
How do I dynamically take variables from previous plays in Ansible?
Dawny33
# How do I dynamically take variables from previous plays in Ansible?

(Asked by: username_0 on 2017-05-05)

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?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2017-05-05. Score: 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.

> **Comments:**
> **username_0**: Seems to work.  But, this [small issue](https://www.reddit.com/r/ansible/comments/69xhkw/why_is_my_set_fact_play_running_even_when_the/?st=j2g3fluo&sh=7c840e20) is roadblocking the entire thing :D . Possible to know how to stop the failed `set_facts` tasks from setting empty values to it?  
> **username_1**: 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 ?  

---

