2015-10-16 103 views
-2

我正在嘗試使用我所有的AWS工具學習Ansible。因此,我想要做的第一個任務是創建裝入卷的基本EC2實例。 我根據Ansible文檔編寫了Playbook,但它確實不起作用。 我的劇本:如何使用Ansible爲EC2實例添加和安裝卷

# The play operates on the local (Ansible control) machine. 
- name: Create a basic EC2 instance v.1.1.0 2015-10-14 
    hosts: localhost 
    connection: local 
    gather_facts: false 

# Vars. 
    vars: 
     hostname: Test_By_Ansible 
     keypair: MyKey 
     instance_type: t2.micro 
     security_group: my security group 
     image: ami-d05e75b8     # Ubuntu Server 14.04 LTS (HVM) 
     region: us-east-1     # US East (N. Virginia) 
     vpc_subnet_id: subnet-b387e763  
     sudo: True 
     locale: ru_RU.UTF-8 

# Launch instance. Register the output. 
    tasks: 
    - name: Launch instance 
     ec2: 
     key_name: "{{ keypair }}" 
     group: "{{ security_group }}" 
     instance_type: "{{ instance_type }}" 
     image: "{{ image }}" 
     region: "{{ region }}" 
     vpc_subnet_id: "{{ vpc_subnet_id }}" 
     assign_public_ip: yes 
     wait: true 
     wait_timeout: 500 
     count: 1       # number of instances to launch 
     instance_tags: 
      Name: "{{ hostname }}" 
      os: Ubuntu 
      type: WebService 
     register: ec2 

    # Create and attach a volumes. 
    - name: Create and attach a volumes 
     ec2_vol: 
     instance: "{{ item.id }}" 
     name: my_existing_volume_Name_tag 
     volume_size: 1 # in GB 
     volume_type: gp2 
     device_name: /dev/sdf 
     with_items: ec2.instances 
     register: ec2_vol 

    # Configure mount points. 
    - name: Configure mount points - mount device by name 
     mount: name=/system src=/dev/sda1 fstype=ext4 opts='defaults nofail 0 2' state=present 
     mount: name=/data src=/dev/xvdf fstype=ext4 opts='defaults nofail 0 2' state=present 

但這個劇本上的卷擊碎安裝錯誤:

fatal: [localhost] => One or more undefined variables: 'item' is undefined 

可能有人誤會我伸出援助之手嗎?

回答

1

你似乎一次複製/粘貼了很多東西,而不是需要一些可以幫助你的信息,你需要去學習Ansible的基礎知識,這樣你才能想到通過在這個劇本中不匹配的所有單獨位。

讓我們來看看您正在碰到的具體錯誤 - item is undefined。它在這裏引發的:

# Create and attach a volumes. 
- name: Create and attach a volumes 
    ec2_vol: 
    instance: "{{ item.id }}" 
    name: my_existing_volume_Name_tag 
    volume_size: 1 # in GB 
    volume_type: gp2 
    device_name: /dev/sdf 
    with_items: ec2.instances 
    register: ec2_vol 

該任務旨在通過列表中的每個項目進行循環,在這種情況下該列表是ec2.instances。它不是,因爲with_items應該是縮進的,所以它與寄存器保持一致。

如果你有一個實例列表(你不知道),它會使用id作爲{{ item.id }}行中的每一個......但是可能會出現一個錯誤,因爲我不認爲他們會被允許擁有相同的名字。

出門學習,你可以弄清楚這種細節。