2015-07-20 15 views
6

Bir AWS EC2 yanıtlayıcı' taktik kitabı oluşturmak çalışıyorum vurularak üzerinde US batı-1, ap-kuzeydoğu-1 ve ab-batı-1.yanıtlayıcı ': o birinde birden fazla bölgede birden EC2 örneğini oluşturmak için çalışılıyor

2), her bir bölge (ec2_ami_search), en son ubuntu AMI bulur

3) daha sonra, 1 ila tespit sonuçları kullanarak) ve 2), için (son ubuntu AMI ile bölge için bir EC2 örneği oluşturmak , Kullanılabilirlik Bölgeleri us-west-1a, ap-kuzeydoğu-1a ve eu-west-1a'dır.

yanıtlayıcı 'ile

, bir aşama 1 ile sorun vardı) ve 2) sadece olan:

> 

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

ve hata modülü ile ubuntu_image değişken için çıkışı: I Ancak

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" 
      } 
     ] 
    } 
} 

adım 3) 'un ubuntu_image register değişkeninden sonucunu nasıl alacağını anlayamadım ve daha sonra verilen EC2 örneğinin hangi AMI ve Subnet'lerden hangisinin ait olduğunu belirleyin. Eğer için bir çözüm düşünebiliriz, ami/alt ağ çalışmalarını hardcoding iken

- 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 

: geçici bir çözüm olarak el ı sadece yukarıdaki ubuntu_image çıktısından çıktısından var ami ve alt ağ değerini kodlanmış nerede aşağıya bakın ami/subnet'in bu kodlamasını önlemek için? o özelleştirmek için gerçekten çok kolay yüzden, bu Ansible bir "plugable" sistemdir unutmayın

cevap

4

değer eşleştirmeleri "ami bölge" nin bir sözlük haline alamadım gibi Ben boşuna set_fact karıştırmasını çalıştı senin özün için. Bazen "yerel" modülleri kullanarak bir geçici çözüm bulmaya çalışmaktan bile daha kolay ve hızlıdır.

Sizin durumunuzda, subnet numaralı doğru bir şekilde kendi özel lookup_plugin kodunuzu yazabilirsiniz. Örneğin

:

  1. ana klasörde lookup_plugins adlı bir klasör oluşturun. (Hesabınız yoksa)
  2. ansible.cfg
[defaults] 
lookup_plugins = lookup_plugins 

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] 

yukarıdaki basit kod arardım denilen lookup_plugins bir dosya oluşturun adlı bir dosya oluşturun Belirli bir bölgede bir alt ağ. Tabi ki istediğiniz gibi özelleştirebilirsiniz.senin oyun kitabı referans Sonra

bu eklenti

doğru alt ağ bulmak için:

Örnek: Senin durumunda

- 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') }}" 

muhtemelen doğru AMI ve ilgili Region başvurmak gerekir.

0

hala sunucuların modülo '%' ve alt ağlar uzunluğunu hesaplayabilirsiniz başka modül yardımı olmadan bunu yapmak istiyorum:

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

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

görev:

- 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}}" 
İlgili konular