snippetMinor
How do I dynamically take variables from previous plays in Ansible?
Viewed 0 times
previousdynamicallytakeplaysvariableshowfromansible
Problem
Example, I have 3 tasks, which are registered
Now, I have another task below it which is :
where
So, how do I set
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?Solution
It feels dirty, but I guess something like that would work:
Basically setting the
- 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.Code Snippets
- 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 }}"Context
StackExchange DevOps Q#1110, answer score: 5
Revisions (0)
No revisions yet.