如何使用Ansible将卷附加到其实例的私有IP地址标记卷

时间:2016-12-15 21:20:49

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

我正在尝试使用Ansible的安装模块在许多实例(具有特定名称)上安装一堆卷,我需要一种方法来确保我可以检查正在解析的卷是否转到正确的实例安装。

我遇到的问题是,如果我有两台主机和这些主机的实例信息(包括卷ID),它会尝试将所有卷挂载到两台主机,实际上我需要检查这会阻止这种情况。

我希望能够使用已附加的实例的IP地址标记卷。我可以用一个实例来完成它,但它不适用于多个实例......

有一个实例:

- name: Tag the EBS Volumes
  hosts: datanodes
  gather_facts: False
  tags: tag
  vars_files:
    - /etc/ansible/defaults.yml         
  tasks:
  - setup:
        filter: ansible_mounts

  - name: Gather datanodes instance instance_ids
    local_action:
        module: ec2_remote_facts
        region: '{{ aws_region }}'
        filters:
            instance-state-name: running
            "tag:Type": datanodes
    register: dn_id

  - name: Verify that the datanodes instance is running...
    local_action:
        module: ec2
        region: '{{ aws_region }}'
        instance_ids: '{{ item.id }}'
        state: running
        wait: True
        vpc_subnet_id: '{{ vpc_subnet_id }}'
    tags: start    
    register: ec2
    with_items: "{{ dn_id.instances }}"
  - debug: var=ec2

  - name: Gather volume information for dn instance
    local_action:
        module: ec2_vol
        region: '{{ aws_region }}'
        instance: '{{ item.id }}'
        state: list
    register: volume
    with_items: "{{ ec2.results[0].instances }}"

  - name: tag the volumes with their correct instance IP Address
    local_action: 
        module: ec2_tag 
        resource: '{{ item.1.id }}'
        region: "{{ aws_region }}"
        tags:
          CurrentIP: '{{ item.0 }}'
          #DeviceName: '{{ item.0 }}'
          #VolumeId: '{{ item.1.volume_id }}'
          #MountName: '{{ item.1.mount }}'
    with_nested:
        - "{{ groups.datanodes }}"      
        - "{{ volume.results[0].volumes }}"

  - name: tag the volumes with the universal volume Id (everyone gets this one)
    local_action: 
        module: ec2_tag 
        resource: '{{ item.0.id }}'
        region: "{{ aws_region }}"
        tags:
           home_id: '{{ item.1.id }}'
          #CurrentIP: '{{ item.0 }}'
          #DeviceName: '{{ item.0 }}'
          #VolumeId: '{{ item.1.volume_id }}'
          #MountName: '{{ item.1.mount }}'
    with_nested:   
        - "{{ volume.results[0].volumes }}"
        - "{{ volume.results[0].volumes }}"
    when: item.device_name | search("/dev/sd")

如何在多个实例中执行此操作?如果你有一个更好的方法,一个节点,那将是伟大的。另外,我需要它尽可能动态,因此我不能只为每个名称使用/dev/sdb/mount1 ...

1 个答案:

答案 0 :(得分:0)

我能够弄清楚标记实际实例和音量。但是,卷标记有其局限性,因为我似乎不得不假设卷首先标记了它的卷ID。不过,它确实有用......

  - name: Tag the Volumes with their Correct db Instance IP Address
    local_action: 
        module: ec2_tag 
        resource: '{{ item.tags.VolumeId }}'
        region: "{{ aws_region }}"
        tags:
          CurrentIP: '{{ item.private_ip_address }}'
          #DeviceName: '{{ item.0 }}'
          #VolumeId: '{{ item.1.volume_id }}'
          #MountName: '{{ item.1.mount }}'
    with_items: "{{ db_id.instances }}"

  - name: Tag the Instances with their Correct db Instance IP Address
    local_action: 
        module: ec2_tag 
        resource: '{{ item.id}}'
        region: "{{ aws_region }}"
        tags:
          CurrentIP: '{{ item.private_ip_address }}'
          HDeviceName: '{{ home_vol_device }}'
          HMountName: '{{ home_vol_mount_name }}'
    with_items: "{{ db_id.instances }}"