0

目標是旋轉多個實例,可以使用count實現,但我已經給出了特定範圍的私有IP地址,並且希望將它們分配給實例。如何啓動多個aws實例並使用ansible來指定給定範圍的IP地址?

下面是我現在的劇本,

--- 
    - name: Provision an EC2 Instance 
    hosts: local 
    connection: local 
    gather_facts: False 
    tags: provisioning 
    # Necessary Variables for creating/provisioning the EC2 Instance 
    vars: 
     instance_type: t2.micro 
     security_group: default # Change the security group name here 
     image: ami-a9d276c9 # Change the AMI, from which you want to launch the server 
     region: us-west-2 # Change the Region 
     keypair: ansible # Change the keypair name 
     ip_addresses: 
     - 172.31.1.117/32 
     - 172.31.1.118/32 
     count: 2 

    tasks: 

     - name: Launch the new EC2 Instance 
     local_action: ec2 
         group={{ security_group }} 
         instance_type={{ instance_type}} 
         image={{ image }} 
         wait=true 
         region={{ region }} 
         keypair={{ keypair }} 
         count={{count}} 
         vpc_subnet_id=subnet-xxxxxxx 
#      private_ip={{private_ip}} 
     with_items: ip_addresses 
     register: ec2 

     - name: Wait for SSH to come up 
     local_action: wait_for 
         host={{ item.public_ip }} 
         port=22 
         state=started 
     with_items: ec2.instances 

     - name: Add tag to Instance(s) 
     local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present 
     with_items: ec2.instances 
     args: 
      tags: 
      Name: ansible 

     - name: Update system 
     apt: update_cache=yes 

     - name: Install Git 
     apt: 
      name: git 
      state: present 

     - name: Install Python2.7 
     apt: 
      name: python=2.7 
      state: present 

     - name: Install Java 
     apt: 
      name: openjdk-8-jdk 
      state: present 

哪家雖然造就了實例,但不分配打算分配的IP地址。我得到以下警告

PLAY [Provision an EC2 Instance] *********************************************** 

TASK [Launch the new EC2 Instance] ********************************************* 
changed: [localhost -> localhost] => (item=172.31.1.117/32) 
changed: [localhost -> localhost] => (item=172.31.1.118/32) 
[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this will be a fatal error.. This feature will be removed in a future release. Deprecation warnings can be 
disabled by setting deprecation_warnings=False in ansible.cfg. 

請建議我最好的方式來實現這一點。

回答

0
  • 你給count=2,所以2個實例將啓動
  • 您的IP地址是錯誤的,你給的,而不是IP一個CIDR
  • 啓動時,您使用的不是在你的代碼的任何地方的IP地址實例

如何解決?

ip_addresses: 
    - 172.31.1.117 
    - 172.31.1.118 
  • 不要在ec2模塊
  • 循環指定count通過ipaddresses的名單(有其中2)
  • 確保您使用的IP通過引用{item}

喜歡這個:

private_ip={{item}}