BTW, the ec2_instances variable in my solution is the registered result of 
the ec2 task run with 'with_items'.

On Thursday, March 29, 2018 at 2:28:50 PM UTC-6, Tim Stewart wrote:
>
> Here is the solution that I came up with through a good deal of trial and 
> error:
>
> vars:
>
>     # The list of service names we will be deploying.
>     services:
>       - name: microservice-1
>         port: 8080
>       - name: microservice-2
>         port: 8080
>       - name: microservice-3
>         port: 8080
>      :
>      :
>       - name: microservice-8
>         port: 8080
>
> tasks:
>
>     # some tasks omitted
>
>     - name: Create Target Groups
>       elb_target_group:
>         region: "{{ region }}"
>         name: "{{ item.name }}-tg"
>         protocol: http
>         port: 80
>         vpc_id: "{{ vpc_id }}"
>         health_check_path: /health
>         health_check_interval: 60
>         successful_response_codes: "200"
>         targets: "{{ ec2_instances | json_query(query) | map('combine', 
> {'Port': item.port}) | list }}"
>         tags:
>           service: "{{ item.name }}"
>         state: present
>       register: target_groups
>       with_items: "{{ services }}"
>       vars:
>         query: 
> "results[].tagged_instances[?tags.service=='{{item.name}}'][].{Id: 
> id}"
>
>
> On Wednesday, March 28, 2018 at 4:50:01 PM UTC-6, Tim Stewart wrote:
>>
>> Hi,
>>
>> *Background:*
>>
>> We are developing a system using a Microservices approach and deploying 
>> it with Ansible 2.5.0.
>>
>> I'm writing a site.yml playbook that will provision two Ubuntu 16.04 
>> servers for each of eight different microservices.  We're provisioning two 
>> of each microservice for HA and we may increase that number in the future.  
>> For example, here are the microservice instances I want to create grouped 
>> by subnet (note this is not YAML):
>>
>> SubNet-a:
>>     MicroService-1-a
>>     MicroService-2-a
>>     :
>>     :
>>     MicroService-8-a
>>
>> SubNet-b:
>>     MicroService-1-b
>>     MicroService-2-b
>>     :
>>     :
>>     MicroService-8-b
>>
>> Now I want to create a Target Group for each kind of microservice (e.g. 
>> one for MicroService-1, one for Microservice-2, etc.).  For example, here 
>> are the target groups and their targets:
>>
>> MicroService-1-TargetGroup:
>>    MicroService-1-a
>>    MicroService-1-b
>>    
>> MicroService-2-TargetGroup:
>>    MicroService-2-a
>>    MicroService-2-b
>>
>> :
>> :
>>
>> MicroService-8-TargetGroup:
>>    MicroService-8-a
>>    MicroService-8-b
>>
>> I then want to set up my ALB (using the elb_application_lb module) so 
>> that it has a *listener* with eight *rules* that route requests to the 
>> various Target groups based on a *path_pattern* condition.
>>
>> Here is an example *elb_target_group* with hard-coded targets (based on 
>> Ansible's documentation on elb_target_groups):
>>
>> - elb_target_group:
>>     name: Microservice-1
>>     protocol: http
>>     port: 81
>>     vpc_id: vpc-01234567
>>     health_check_path: /
>>     successful_response_codes: "200"
>>     targets:*      - Id: i-01234567
>>         Port: 80
>>       - Id: i-98765432
>>         Port: 80*
>>     state: present
>>     wait_timeout: 200
>>     wait: True
>>
>>
>> But since my site.yml playbook creates the EC2 instances, I won't have 
>> access to the *instance_id*s referred to in the *Id* parameter above.
>>
>> What is a good way of specifying the *targets* property of the 
>> *elb_target_group* (above) such that it only includes all of the EC2 
>> instances of a particular kind of microservice?
>>
>> For example:
>>
>> - elb_target_group:
>>     name: *Microservice-1*
>>     protocol: http
>>     port: 81
>>     vpc_id: vpc-01234567
>>     health_check_path: /
>>     successful_response_codes: "200,250-260"
>>     targets:      - Id: *< **Microservice-1-a's instance_id >*
>>         Port: 80
>>       - Id:* < **Microservice-2-a's instance_id >*
>>         Port: 80
>>     state: present
>>     wait_timeout: 200
>>     wait: True
>>
>>
>> Thanks!
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d8b3aa27-555a-4a2d-99ca-6cbbbee7f27e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to