Here I am trying to perform the below-listed activities
- Deregister Instance from Application Load Balancer
- Deploy Application
- Register back the same instance on the Application Load Balancer
The use case is to add back the deregistered instance back to the same ALB target groups from where it was removed. While deregistering the instance I am saving the Target group ARN in {{ my_target_arn }}
and later using it in a different role to register it back the same instance in the Target groups.
Below is the code for deregistering Instance from ALB:
---
- name: Get EC2 instance ID
ec2_instance_info:
filters:
private-ip-address: "{{ ansible_host }}"
register: ec2_details
- name: Get list of target groups with the EC2
delegate_to: localhost
elb_target_info:
instance_id: "{{ ec2_details.instances[0].instance_id }}"
register: target_info
- name: Deregister the EC2
delegate_to: localhost
elb_target:
target_group_arn: "{{ item.target_group_arn }}"
target_id: "{{ ec2_details.instances[0].instance_id }}"
deregister_unused: yes
target_status_timeout: 10
state: absent
with_items: "{{ target_info.instance_target_groups }}"
register: detached_details
when: target_info != ""
- set_fact:
my_target_arn: "{{ item.target_group_arn }}"
with_items: "{{ target_info.instance_target_groups }}"
when: target_info != ""
Below listed is the code for adding back the instance:
---
- name: Get EC2 instance ID
ec2_instance_info:
filters:
private-ip-address: "{{ ansible_host }}"
register: ec2_details
- name: Register the EC2
delegate_to: localhost
elb_target:
target_group_arn: "{{ item }}"
target_id: "{{ ec2_details.instances[0].instance_id }}"
deregister_unused: yes
target_status_timeout: 10
state: present
with_items: "{{ my_target_arn }}"
register: attached_details
when: my_target_arn != ""
Playbook code:
---
- name: Check EC2 details
hosts: appserver
roles:
- roles/ec2_deregister
- roles/deploy_application
- roles/ec2_register
Here the issue is, my {{ ansible_host }}
is associated with multiple Target groups. So while deregistering the instance, it deregisters instance from these multiple target groups. However, while registering back the instance, it only adds it back to a single target group(the first one which it gets).
So basically I want to set_fact
with a list variable that might or might not have multiple values(It can be a single value or a list of values). Please suggest how to set_fact
with list variables.