【问题标题】:How to add and mount volumes for EC2 instance with Ansible如何使用 Ansible 为 EC2 实例添加和挂载卷
【发布时间】:2020-12-11 06:12:24
【问题描述】:

我正在尝试使用我所有的 AWS 知识来学习 Ansible。所以我要做的第一个任务是创建带有挂载卷的基本 EC2 实例。

我根据 Ansible 文档编写了 Playbook,但它并没有真正起作用。我的剧本:

# 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
      

但是这个 Playbook 会因错误而在卷挂载上崩溃:

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

我该如何解决这个问题?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 ansible


    【解决方案1】:

    您似乎一次复制/粘贴了很多东西,而不是需要 SO 可以帮助您的特定信息,您需要开始学习 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 }} 行中的每个实例......但随后可能抛出一个错误,因为我不认为他们都被允许有相同的名字。

    去研究,你可以弄清楚这种细节。

    【讨论】:

    • 答案并不完整,因为没有提到如何挂载添加的设备。
    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 2017-03-02
    • 2017-01-31
    • 2016-03-30
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多