簡體   English   中英

如何使用 os_volume_snapshot 模塊編寫劇本以刪除超過 10 天的 OpenStack 卷快照

[英]how to write a playbook to delete OpenStack volume snapshot of older than 10 days using os_volume_snapshot module

---
- name: Creating a volume snapshot
  hosts: Test-ctrl
  gather_facts: True
  tasks:
  - name: Creating snapshot of Test
    os_volume_snapshot:
      auth:
        auth_url: http://20.10.X.X:5000/v3/
        username: XXXXXXX
        password: XCXCXCXC
        project_name: test-stack
        project_domain_name: Default
        user_domain_name: Default
      state: absent
      validate_certs: False
      display_name: Test- {{ lookup('pipe','date +%Y-%m-%d-%H-%M-%S') }}
      volume: Test-1
      force: yes

如何編寫劇本來刪除超過 10 天的 OpenStack 卷快照

這是我創建音量的劇本。 但自定義刪除超過 10 天或 5 天的卷????

我也需要這樣做,但遺憾的是,使用os_volume_snapshot模塊是不可能的。 也不可能使用 Ansible (2.9) 中的任何 OpenStack 模塊。 此外, os_volume_snapshot使volume參數成為強制性參數(這很愚蠢 - 因為您不需要知道原始卷的名稱即可刪除快照)。

所以,如果你“必須”使用os_volume_snapshot那么你就不走運了。

Ansible 中內置的os_模塊在對 OpenStack 集群的全面控制方面非常“進行中”,對於您已確定的任務幾乎沒有用處。

但是……等等……

像你一樣,我需要自動化這個,它可以使用 Ansible 和官方python-openstackclient模塊來完成。 好的 - 它不是“純粹的”Ansible(即它不使用純粹的內置模塊),但它是一個使用內置command模塊的劇本並且它可以工作。

(順便說一句 - 以下不提供任何保證 - 使用風險自負)

因此,這是我正在運行的一個劇本,它刪除已達到定義年齡的快照卷 days 您將需要提供以下變量...

是的,有更好的方法來提供 OpenStack 變量(如雲文件或OS_環境變量等),但我已經明確了所有需要的變量,因此更容易知道實際需要什么。

  • os_auth_url (即“https://example.com:5000/v3”)
  • os_username
  • os_password
  • os_project_name
  • os_project_domain_name (即“默認”)
  • os_user_domain_name

  • retirement_age_days +ve 天數,其中所有已達到該期限的快照卷都將被刪除。 如果為 0,則刪除所有快照。

劇本所做的總結: -

  1. 它使用openstack volume snapshot list來獲取項目中的快照卷列表
  2. 然后它使用openstack volume snapshot show來獲取關於每個快照的信息(即它的created_at日期)並構建一個卷年齡(以天為單位)
  3. 然后它使用openstack volume snapshot delete來刪除所有被認為太舊的卷
---

- hosts: localhost
  tasks:

  # Delete all project snapshots that are too old.

  # The user is expected to define the variable 'retirement_age_days'
  # where all volumes that have reached that age are deleted.
  # If 0 all snapshots are deleted.
  #
  # The user is also required to define OpenStack variables.

  - name: Assert control variables
    assert:
      that:
      - retirement_age_days is defined
      - retirement_age_days|int >= 0
      - os_auth_url is defined
      - os_username is defined
      - os_password is defined
      - os_project_name is defined
      - os_project_domain_name is defined
      - os_user_domain_name is defined

  # Expectation here is that you have the following OpenStack information: -
  #
  # - auth_url (i.e. "https://example.com:5000/v3")
  # - username
  # - password
  # - project_name
  # - project_domain_name (i.e. "Default")
  # - user_domain_name

  # We rely in the OpenStack client - the Ansible "os_" module
  # (Ansible 2.9) does not do what we need, so we need the client.
  # It' Python so make sure it's available...

  - name: Install prerequisite Python modules
    pip:
      name:
      - python-openstackclient==5.3.1
      extra_args: --user

  - name: Set snapshot command
    set_fact:
      snapshot_cmd: openstack volume snapshot

  # To avoid cluttering the command-line we
  # define all the credential material as a map of variables
  # that we then apply as the 'environment' for each command invocation.

  - name: Define OpenStack environment
    set_fact:
      os_env:
        OS_AUTH_URL: "{{ os_auth_url }}"
        OS_USERNAME: "{{ os_username }}"
        OS_PASSWORD: "{{ os_password }}"
        OS_PROJECT_NAME: "{{ os_project_name }}"
        OS_PROJECT_DOMAIN_NAME: "{{ os_project_domain_name }}"
        OS_USER_DOMAIN_NAME: "{{ os_user_domain_name }}"

  # Get all the snapshot names in the project.
  # The result is a json structure that we parse
  # in order to get just the names...

  - name: Get snapshots
    command: "{{ snapshot_cmd }} list --format json"
    environment: "{{ os_env }}"
    changed_when: false
    register: snap_result

  - name: Collect snapshot volume names
    set_fact:
      snap_volume_names: "{{ snap_result.stdout|from_json|json_query(query)|flatten }}"
    vars:
      query: "[*].Name"

  - name: Display existing snapshot volumes
    debug:
      var: snap_volume_names

  # For each snapshot, get its 'info'.
  # The combined results are then parsed in order to
  # locate each volume's 'created_at' date.
  # We compare that to 'now' in order to build a list of
  # volume ages (in days)...

  - name: Get snapshot volume info
    command: "{{ snapshot_cmd }} show {{ item }} --format json"
    environment: "{{ os_env }}"
    changed_when: false
    register: snap_volume_info
    loop: "{{ snap_volume_names }}"

  - name: Create snapshot age list (days)
    set_fact:
      snap_volume_ages: >-
        {{
          snap_volume_ages|default([]) +
          [ ((ansible_date_time.iso8601|to_datetime(fmt_1)) -
             (item.stdout|from_json|json_query(query)|to_datetime(fmt_2))).days ]
        }}
    vars:
      query: "created_at"
      fmt_1: "%Y-%m-%dT%H:%M:%SZ"
      fmt_2: "%Y-%m-%dT%H:%M:%S.%f"
    loop: "{{ snap_volume_info.results }}"

  # Using the combined volume names and ages lists
  # iterate through the ages and delete volumes have reached their age limit...

  - name: Delete old snapshots
    command: "{{ snapshot_cmd }} delete {{ item.1 }}"
    environment: "{{ os_env }}"
    changed_when: true
    when: item.0 >= retirement_age_days|int
    with_together:
    - "{{ snap_volume_ages|default([]) }}"
    - "{{ snap_volume_names|default([]) }}"

暫無
暫無

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

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