如何编写一本使用os_volume_snapshot模块删除超过10天的OpenStack卷快照的剧本


---
- 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,那么你就倒霉了。

内置的CCD_ 5模块在很大程度上是一个";进行中的工作";关于Ansible中OpenStack集群的全面控制,它对您确定的任务几乎没有用处。

但是。。。等等

和您一样,我需要将其自动化,并且可以使用Ansible和官方python-openstackclient模块来完成。好吧,这不是";纯";Ansible(也就是说,它没有使用纯内置模块(,但它是一个使用内置command模块的剧本,它是有效的。

(BTW-以下不提供保修-使用风险自负(

因此,下面是我正在运行的一个操作手册,将删除在天内达到定义期限的快照卷。您需要提供以下变量。。。

是的,有更好的方法来提供OpenStack变量(如云文件或OS_环境变量等(,但我已经明确了所有需要的变量,因此更容易知道实际需要什么。

  • CCD_ 9(即;https://example.com:5000/v3"(
  • os_username
  • os_password
  • os_project_name
  • CCD_ 13(即"默认"(
  • 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([]) }}"

最新更新