1

Here I am trying to perform the below-listed activities

  1. Deregister Instance from Application Load Balancer
  2. Deploy Application
  3. 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.

Lijo Abraham
  • 304
  • 1
  • 2
  • 17

2 Answers2

1

what version of ansible are you using?

I tried setting a fact in one role and passing it to the other, seems to work just fine.

I am using ansible 2.9.6

role1:

- set_fact:
    my_fact: test

role2:

- debug:
    var: my_fact

playbook:

---
- hosts: localhost
  gather_facts: no
  become: no
  roles:
    - ./roles/role1
    - ./roles/role2

and the output:

TASK [./roles/role1 : set_fact] 
ok: [localhost]

TASK [./roles/role2 : debug] 
ok: [localhost] => {
    "my_fact": "test1234"
}
liad9122
  • 291
  • 2
  • 3
  • 9
  • Ansible version `ansible 2.9.6` I was able to access the fact in another role. However, the variable can also be a list of values. help me with passing list values to another role. I have edited the question. – Lijo Abraham May 09 '20 at 22:28
0

Was able to get the list using the below-mentioned code

Deregister Instance

---
- 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

- set_fact:
    my_target_arn: "{{ my_target_arn|default([]) + [{'target_group_arn': item}] }}"
  loop: "{{ target_info.instance_target_groups|map(attribute='target_group_arn')|list }}"
  when: 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 != ""

Register back the same instance

---
- name: Get EC2 instance ID
  ec2_instance_info:
    filters:
      private-ip-address: "{{ ansible_host }}"
  register: ec2_details
  when: my_target_arn is defined

- name: Register 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: present
  with_items: "{{ my_target_arn }}"
  register: attached_details
  when: my_target_arn is defined
Lijo Abraham
  • 304
  • 1
  • 2
  • 17