簡體   English   中英

如何使用Ansible使用附加到實例的私有IP地址標記卷

[英]How do I tag a volume with the private IP address of the instance it is attached to using 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 ...

通過標記實際實例和卷,我能夠弄清楚。 但是,卷標記有其局限性,因為似乎我不得不假定該卷首先是用卷的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 }}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM