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=query或- state=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(新版本文件夹的路径(不是由模块创建的))。
参数
| 参数 | 注释 | 
|---|---|
| 生成的 filesystem 对象应具有的属性。 要获取支持的标志,请查看目标系统上  此字符串应包含与  
 | |
| 完成部署时创建的符号链接的名称。用于  默认值:  | |
| 应该拥有 filesystem 对象的组的名称,就像提供给  如果未指定,则它使用当前用户的当前组,除非您是 root 用户,在这种情况下,它可以保留以前的拥有权。 | |
| 清理时要保留的旧版本的数量。用于  默认值:  | |
| 生成的 filesystem 对象应具有的权限。 对于习惯使用  如果向 Ansible 提供数字但不遵循以上任一规则,最终将得到一个小数,这将导致意外的结果。 从 Ansible 1.8 开始,模式可以指定为符号模式(例如, 如果未指定 如果未指定 指定 | |
| 应该拥有文件系统对象的用户的名称,如同传递给 如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,它可以保留之前的拥有者。 指定数字用户名将被视为用户 ID 而不是用户名。避免使用数字用户名以避免混淆。 | |
| 项目的根路径。返回在 | |
| 正在部署的版本。默认为时间戳格式 | |
| 将保存版本的文件夹名称。这可以相对于 默认值: | |
| SELinux 文件系统对象上下文的级别部分。 这是 MLS/MCS 属性,有时称为 设置为 | |
| SELinux 文件系统对象上下文的角色部分。 设置为 | |
| SELinux 文件系统对象上下文的类型部分。 设置为 | |
| SELinux 文件系统对象上下文的用户部分。 默认情况下,它使用 设置为 | |
| 将保存共享资源的文件夹名称。这可以相对于 默认值: | |
| 项目的状态。 
 
 
 
 
 选项 
 | |
| 指示部署尚未完成的文件的名称。在 默认值: | |
| 影响何时使用原子操作来防止目标文件系统对象的数据损坏或不一致读取。 默认情况下,此模块使用原子操作来防止目标文件系统对象的数据损坏或不一致读取,但有时系统配置或损坏的方式会阻止此操作。一个示例是 docker 挂载的文件系统对象,无法从容器内部以原子方式更新,只能以不安全的方式写入。 此选项允许 Ansible 在原子操作失败时回退到不安全的文件系统对象更新方法(但是,它不会强制 Ansible 执行不安全写入)。 重要!不安全写入容易出现竞争条件,并可能导致数据损坏。 选项 
 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持:完全支持 | 可以在 | |
| 支持:不支持 | 在差异模式下,将返回有关已更改内容(或可能需要在 | 
备注
注意
- 事实仅在 - state=query和- state=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
