2015-07-20 11 views
6

Próbuję utworzyć ansibl playbook AWS EC2, że:ansibl: Próbuję utworzyć wiele instancji EC2 w wielu regionach w jednym ujęciu

1) najpierw przydziela jeden VPC każdym z trzech regionów, które są: US- west-1, ap-northeast-1 i eu-west-1.

2) Znajduje ostatnią ubuntu AMI dla każdego regionu (ec2_ami_search)

3), a następnie za pomocą wykryte wyników z 1) i 2), utworzyć jedną instancję EC2 danym regionie z najnowszymi ubuntu AMI (na region) z dostępnymi strefami nas-west-1a, ap-northeast-1a i eu-west-1a, odpowiednio.

z ansibl, że nie ma problemu z etapu 1) i 2), który jest po prostu:

> 

    tasks: 
    - name: create a vpc 
    ec2_vpc: 
     state: present 
     region: "{{ item.region }}" 
     internet_gateway: True 
     resource_tags: { env: production} 
     cidr_block: 10.0.0.0/16 
     subnets: 
     - cidr: 10.0.0.0/24 
      az: "{{ item.az }}" 
      resource_tags: 
      env: production 
      tier: public 
     route_tables: 
     - subnets: 
      - 10.0.0.0/24 
      routes: 
      - dest: 0.0.0.0/0 
      gw: igw 
    with_items: 
     - region: us-west-1 
     az: us-west-1a 
     - region: ap-northeast-1 
     az: ap-northeast-1a 
     - region: eu-west-1 
     az: eu-west-1a 
... 
    - name: Get the ubuntu trusty AMI 
    ec2_ami_search: distro=ubuntu release=trusty virt=hvm region={{ item }} 
    with_items: 
     - us-west-1 
     - ap-northeast-1 
     - eu-west-1 
    register: ubuntu_image 
... 
> 

i wyprowadzany zmiennej ubuntu_image z modułem debugowania:

TASK: [print out ubuntu images] *********************************************** 
ok: [localhost] => { 
    "ubuntu_image": { 
     "changed": false, 
     "msg": "All items completed", 
     "results": [ 
      { 
       "aki": null, 
       "ami": "ami-b33dccf7", 
       "ari": null, 
       "changed": false, 
       "invocation": { 
        "module_args": "distro=ubuntu release=trusty virt=hvm region=us-west-1", 
        "module_name": "ec2_ami_search" 
       }, 
       "item": "us-west-1", 
       "serial": "20150629", 
       "tag": "release" 
      }, 
      { 
       "aki": null, 
       "ami": "ami-9e5cff9e", 
       "ari": null, 
       "changed": false, 
       "invocation": { 
        "module_args": "distro=ubuntu release=trusty virt=hvm region=ap-northeast-1", 
        "module_name": "ec2_ami_search" 
       }, 
       "item": "ap-northeast-1", 
       "serial": "20150629", 
       "tag": "release" 
      }, 
      { 
       "aki": null, 
       "ami": "ami-7c4b0a0b", 
       "ari": null, 
       "changed": false, 
       "invocation": { 
        "module_args": "distro=ubuntu release=trusty virt=hvm region=eu-west-1", 
        "module_name": "ec2_ami_search" 
       }, 
       "item": "eu-west-1", 
       "serial": "20150629", 
       "tag": "release" 
      } 
     ] 
    } 
} 

Jednak nie mógł wymyślić, jak wykonać krok 3) przyjmij wynik ze zmiennej rejestru ubuntu_image , a następnie ustal, który z 3 AMI i podsieci należał do danej instancji EC2. Zobacz poniżej, gdzie jako obejście ręcznie ustalony AMI i podsieci wartość który po prostu dostał z wydrukiem z powyższego ubuntu_image wydruku:

- name: start the instances 
    ec2: 
     image: "{{ item.ami }}" # MANUALLY HARDCODED 
     region: "{{ item.region }}" 
     instance_type: "{{ instance_type }}" 
     assign_public_ip: True 
     key_name: "{{ item.name }}" 
     group: ["http deployment", "ssh deployment", "outbound deployment"] 
     instance_tags: { Name: "{{ item.name }}", type: ss, env: production} 
     exact_count: "{{ count }}" 
     count_tag: { Name: "{{ item.name }}" } 
     vpc_subnet_id: "{{ item.subnet }}" #MANUALLY HARDCODED 
     wait: yes 
    register: ec2 
    with_items: 
     - region: us-west-1 
     name: ss12 
     ami: ami-b33dccf7 # MANUALLY HARDCODED 
     subnet: subnet-35a22550 # MANUALLY HARDCODED 
     - region: ap-northeast-1 
     name: ss21 
     ami: ami-9e5cff9e # MANUALLY HARDCODED 
     subnet: subnet-88c47dff # MANUALLY HARDCODED 
     - region: eu-west-1 
     name: ss32 
     ami: ami-7c4b0a0b # MANUALLY HARDCODED 
     subnet: subnet-23f59554 # MANUALLY HARDCODED 

Podczas hardcoding Ami prace/podsieci, można myśleć roztworu do mnie, aby uniknąć tego twardego kodowania ami/podsieci? Próbowałem aprowizacji z set_fact bezskutecznie, bo nie mógł się on stać się słownikiem „regionu ami” mapowania wartości

Odpowiedz

4

Należy pamiętać, że Ansible to system „wetknięcia”, więc jest to bardzo łatwo dostosować to dla ciebie. Czasami jest to łatwiejsze i szybsze niż znalezienie sposobu na obejście tego problemu za pomocą "rodzimych" modułów.

W twoim przypadku możesz łatwo napisać swój własny custom lookup_plugin, który wyszuka poprawny subnet.

Na przykład:

  1. Utwórz folder o nazwie lookup_plugins w głównym folderze.
  2. Utwórz plik (jeśli nie masz) nazywa ansible.cfg
[defaults] 
lookup_plugins = lookup_plugins 

Utwórz plik w lookup_plugins nazwie subnets.py

import boto.vpc 
class LookupModule(object): 
    def __init__(self, basedir=None, **kwargs): 
     self.basedir = basedir 
     self.plugin_name = 'subnets' 
    def run(self, regions, variable=None, **kwargs): 
     if not isinstance(regions, list): 
      regions = [regions] 
     for region in regions: 
      return [boto.vpc.connect_to_region(region).get_all_subnets()[0].id] 

Powyższy prosty kod będzie szukać podsieć w danym regionie. Oczywiście możesz go dostosować w dowolny sposób.

Następnie w odniesieniu PlayBook tej wtyczki, aby znaleźć prawidłową podsieć:

Przykład:

- hosts: localhost 
    gather_facts: no 
    tasks: 
    - name: Start instance 
     debug: msg="Starting instance {{ item.ami }} in {{ item.region }} in {{ item.subnet }}" 
     with_items: 
     - region: us-west-1 
      name: ss12 
      ami: ami-b33dccf7 
      subnet: "{{ lookup('subnets', 'us-west-1') }}" 
     - region: ap-northeast-1 
      name: ss21 
      ami: ami-9e5cff9e 
      subnet: "{{ lookup('subnets', 'ap-northeast-1') }}" 
     - region: eu-west-1 
      name: ss32 
      ami: ami-7c4b0a0b 
      subnet: "{{ lookup('subnets', 'ap-northeast-1') }}" 

W twoim przypadku to prawdopodobnie trzeba odwołać prawidłowe AMI i związane Region.

0

jeśli nadal chcesz to zrobić bez pomocy z innego modułu można obliczyć modulo „%” serwerów i długość podsieci:

"{{subnets[item.0 | int % subnets | length | int].aws_ec2_subnets}}" 

przykład kod

Vars:

subnets: 
    - {zone: "us-east-1a", aws_ec2_subnets: 'subnet-123'} 
    - {zone: "us-east-1b", aws_ec2_subnets: 'subnet-456'} 
    - {zone: "us-east-1d", aws_ec2_subnets: 'subnet-789'} 

server_list: 
    - server1 
    - server2 
    - server3 

zadanie:

- name: Create new ec2 instance 
    ec2: 
    profile: "{{aws_profile}}" 
    key_name: "{{aws_key_name}}" 
    group_id: "{{aws_security_group}}" 
    instance_type: "{{aws_instance_type}}" 
    image: "{{aws_ami}}" 
    region: "{{region}}" 
    exact_count: "1" 
    #instance_profile_name: none 
    wait: yes 
    wait_timeout: 500 
    volumes: "{{volumes}}" 
    monitoring: no 
    vpc_subnet_id: "{{subnets[item.0 | int % subnets | length | int].aws_ec2_subnets}}" 
    assign_public_ip: no 
    tenancy: default 
    termination_protection: yes 
    instance_tags: 
     App: "{{app_name}}" 
     Environment: "{{environment_type}}" 
     Platform: "{{platform_name}}" 
     Name: "{{item.1}}" 
    count_tag: 
     App: "{{app_name}}" 
     Environment: "{{environment_type}}" 
     Platform: "{{platform_name}}" 
     Name: "{{item.1}}" 
    register: ec2_new_instance 
    with_indexed_items: 
    - "{{server_list}}" 
Powiązane problemy