community.general.deploy_helper 模块 – 管理部署项目中的一些常用步骤

注意

此模块是 community.general 集合 (版本 10.1.0) 的一部分。

如果您正在使用 ansible 包,则您可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general

要在剧本中使用它,请指定:community.general.deploy_helper

概要

  • 部署助手管理部署软件中的一些常用步骤。它创建文件夹结构,管理当前版本的符号链接并清理旧版本。

  • 使用 state=querystate=present 运行它将返回 deploy_helper 事实。project_path(您在 path 参数中设置的值),current_path(指向活动版本的符号链接的路径),releases_path(保存版本的文件夹的路径),shared_path(保存共享资源的文件夹的路径),unfinished_filename(用于检查以识别未完成构建的文件),previous_release(“current”符号链接指向的版本),previous_release_path(“current”符号链接目标的完整路径),new_release(“release”参数或生成的 timestamps),new_release_path(新版本文件夹的路径(不是由模块创建的))。

参数

参数

注释

attributes

别名:attr

字符串

生成的 filesystem 对象应具有的属性。

要获取支持的标志,请查看目标系统上 chattr 的手册页。

此字符串应包含与 lsattr 显示的顺序相同的属性。

= 运算符被假定为默认值,否则需要在字符串中包含 +- 运算符。

clean

布尔值

如果 state=finalize,是否运行清理过程。

选项

  • false

  • true ← (默认值)

current_path

路径

完成部署时创建的符号链接的名称。用于 state=finalizestate=clean。在 deploy_helper.current_path 事实中返回。

默认值: "current"

group

字符串

应该拥有 filesystem 对象的组的名称,就像提供给 chown 一样。

如果未指定,则它使用当前用户的当前组,除非您是 root 用户,在这种情况下,它可以保留以前的拥有权。

keep_releases

整数

清理时要保留的旧版本的数量。用于 state=finalizestate=clean。任何未完成的构建都将首先被删除,因此只有正确的版本才会被计算。当前版本不计入其中。

默认值: 5

mode

任意

生成的 filesystem 对象应具有的权限。

对于习惯使用 /usr/bin/chmod 的用户,请记住模式实际上是八进制数。您必须向 Ansible 提供足够的信息才能正确解析它们。为了获得一致的结果,请引用八进制数(例如,'644''1777'),以便 Ansible 接收一个字符串并可以自行将字符串转换为数字。添加前导零(例如,0755)有时有效,但在循环和其他一些情况下可能会失败。

如果向 Ansible 提供数字但不遵循以上任一规则,最终将得到一个小数,这将导致意外的结果。

从 Ansible 1.8 开始,模式可以指定为符号模式(例如,u+rwxu=rw,g=r,o=r)。

如果未指定mode且目标文件系统对象**不存在**,则在设置新创建的文件系统对象的模式时,将使用系统上的默认umask

如果未指定mode且目标文件系统对象**存在**,则将使用现有文件系统对象的模式。

指定mode是确保以正确的权限创建文件系统对象的最佳方法。有关更多详细信息,请参阅 CVE-2020-1736。

owner(所有者)

字符串

应该拥有文件系统对象的用户的名称,如同传递给chown一样。

如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,它可以保留之前的拥有者。

指定数字用户名将被视为用户 ID 而不是用户名。避免使用数字用户名以避免混淆。

path(路径)

别名:dest

path / required(必需)

项目的根路径。返回在deploy_helper.project_path事实中。

release(版本)

字符串

正在部署的版本。默认为时间戳格式%Y%m%d%H%M%S(例如20141119223359)。此参数在state=present期间是可选的,但对于state=finalize需要显式设置。您可以使用生成的 fact release={{ deploy_helper.new_release }}

releases_path(版本路径)

字符串

将保存版本的文件夹名称。这可以相对于path或绝对路径。返回在deploy_helper.releases_path事实中。

默认值:"releases"

selevel(SELinux 安全级别)

字符串

SELinux 文件系统对象上下文的级别部分。

这是 MLS/MCS 属性,有时称为range

设置为_default时,如果可用,它将使用策略的level部分。

serole(SELinux 安全角色)

字符串

SELinux 文件系统对象上下文的角色部分。

设置为_default时,如果可用,它将使用策略的role部分。

setype(SELinux 安全类型)

字符串

SELinux 文件系统对象上下文的类型部分。

设置为_default时,如果可用,它将使用策略的type部分。

seuser(SELinux 安全用户)

字符串

SELinux 文件系统对象上下文的用户部分。

默认情况下,它使用system策略(如果适用)。

设置为_default时,如果可用,它将使用策略的user部分。

shared_path(共享路径)

路径

将保存共享资源的文件夹名称。这可以相对于path或绝对路径。如果将其设置为空字符串,则不会创建共享文件夹。返回在deploy_helper.shared_path事实中。

默认值:"shared"

state(状态)

字符串

项目的状态。

query 仅收集事实。

present 将创建项目root文件夹,并在其中创建releasesshared文件夹。

finalize 将删除 unfinished_filename 文件,创建一个指向新部署版本的符号链接,并可以选择清除旧版本。

clean 将删除失败的和旧的版本。

absent 将删除项目文件夹(与ansible.builtin.file 模块使用state=absent相同)。

选项

  • "present" ← (默认)

  • "finalize"

  • "absent"

  • "clean"

  • "query"

unfinished_filename(未完成文件名)

字符串

指示部署尚未完成的文件的名称。在state=finalize中使用clean=truestate=clean时,将删除releases_path中包含此文件的所有文件夹。在state=finalize期间,此文件将自动从new_release_path中删除。

默认值:"DEPLOY_UNFINISHED"

unsafe_writes(不安全写入)

布尔值

影响何时使用原子操作来防止目标文件系统对象的数据损坏或不一致读取。

默认情况下,此模块使用原子操作来防止目标文件系统对象的数据损坏或不一致读取,但有时系统配置或损坏的方式会阻止此操作。一个示例是 docker 挂载的文件系统对象,无法从容器内部以原子方式更新,只能以不安全的方式写入。

此选项允许 Ansible 在原子操作失败时回退到不安全的文件系统对象更新方法(但是,它不会强制 Ansible 执行不安全写入)。

重要!不安全写入容易出现竞争条件,并可能导致数据损坏。

选项

  • false ← (默认)

  • true

属性

属性

支持

描述

check_mode(检查模式)

支持:完全支持

可以在check_mode下运行,并在不修改目标的情况下返回更改状态预测。

diff_mode(差异模式)

支持:不支持

在差异模式下,将返回有关已更改内容(或可能需要在check_mode中更改的内容)的详细信息。

备注

注意

  • 事实仅在state=querystate=present时返回。如果您同时使用两者,则应将任何被覆盖的参数传递给这两个调用,否则第二个调用将覆盖第一个调用的事实。

  • 使用state=clean时,版本按*创建日期*排序。您应该能够在没有问题的情况下切换到新的命名策略。

  • 由于生成new_release事实的默认行为,除非您使用release传递您自己的版本名称,否则此模块将不是幂等的。由于软件部署的性质,这应该不是什么大问题。

示例

# General explanation, starting with an example folder structure for a project:

# root:
#     releases:
#         - 20140415234508
#         - 20140415235146
#         - 20140416082818
#
#     shared:
#         - sessions
#         - uploads
#
#     current: releases/20140416082818


# The 'releases' folder holds all the available releases. A release is a complete build of the application being
# deployed. This can be a clone of a repository for example, or a sync of a local folder on your filesystem.
# Having timestamped folders is one way of having distinct releases, but you could choose your own strategy like
# git tags or commit hashes.
#
# During a deploy, a new folder should be created in the releases folder and any build steps required should be
# performed. Once the new build is ready, the deploy procedure is 'finalized' by replacing the 'current' symlink
# with a link to this build.
#
# The 'shared' folder holds any resource that is shared between releases. Examples of this are web-server
# session files, or files uploaded by users of your application. It's quite common to have symlinks from a release
# folder pointing to a shared/subfolder, and creating these links would be automated as part of the build steps.
#
# The 'current' symlink points to one of the releases. Probably the latest one, unless a deploy is in progress.
# The web-server's root for the project will go through this symlink, so the 'downtime' when switching to a new
# release is reduced to the time it takes to switch the link.
#
# To distinguish between successful builds and unfinished ones, a file can be placed in the folder of the release
# that is currently in progress. The existence of this file will mark it as unfinished, and allow an automated
# procedure to remove it during cleanup.


# Typical usage
- name: Initialize the deploy root and gather facts
  community.general.deploy_helper:
    path: /path/to/root
- name: Clone the project to the new release folder
  ansible.builtin.git:
    repo: ansible.builtin.git://foosball.example.org/path/to/repo.git
    dest: '{{ deploy_helper.new_release_path }}'
    version: v1.1.1
- name: Add an unfinished file, to allow cleanup on successful finalize
  ansible.builtin.file:
    path: '{{ deploy_helper.new_release_path }}/{{ deploy_helper.unfinished_filename }}'
    state: touch
- name: Perform some build steps, like running your dependency manager for example
  composer:
    command: install
    working_dir: '{{ deploy_helper.new_release_path }}'
- name: Create some folders in the shared folder
  ansible.builtin.file:
    path: '{{ deploy_helper.shared_path }}/{{ item }}'
    state: directory
  with_items:
    - sessions
    - uploads
- name: Add symlinks from the new release to the shared folder
  ansible.builtin.file:
    path: '{{ deploy_helper.new_release_path }}/{{ item.path }}'
    src: '{{ deploy_helper.shared_path }}/{{ item.src }}'
    state: link
  with_items:
      - path: app/sessions
        src: sessions
      - path: web/uploads
        src: uploads
- name: Finalize the deploy, removing the unfinished file and switching the symlink
  community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Retrieving facts before running a deploy
- name: Run 'state=query' to gather facts without changing anything
  community.general.deploy_helper:
    path: /path/to/root
    state: query
# Remember to set the 'release' parameter when you actually call 'state=present' later
- name: Initialize the deploy root
  community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: present

# all paths can be absolute or relative (to the 'path' parameter)
- community.general.deploy_helper:
    path: /path/to/root
    releases_path: /var/www/project/releases
    shared_path: /var/www/shared
    current_path: /var/www/active

# Using your own naming strategy for releases (a version tag in this case):
- community.general.deploy_helper:
    path: /path/to/root
    release: v1.1.1
    state: present
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Using a different unfinished_filename:
- community.general.deploy_helper:
    path: /path/to/root
    unfinished_filename: README.md
    release: '{{ deploy_helper.new_release }}'
    state: finalize

# Postponing the cleanup of older builds:
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize
    clean: false
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
# Or running the cleanup ahead of the new deploy
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
- community.general.deploy_helper:
    path: /path/to/root
    state: present

# Keeping more old releases:
- community.general.deploy_helper:
    path: /path/to/root
    release: '{{ deploy_helper.new_release }}'
    state: finalize
    keep_releases: 10
# Or, if you use 'clean=false' on finalize:
- community.general.deploy_helper:
    path: /path/to/root
    state: clean
    keep_releases: 10

# Removing the entire project root folder
- community.general.deploy_helper:
    path: /path/to/root
    state: absent

# Debugging the facts returned by the module
- community.general.deploy_helper:
    path: /path/to/root
- ansible.builtin.debug:
    var: deploy_helper

作者

  • Ramon de la Fuente (@ramondelafuente)