如何使用 Ansible 安排网站部署,以便在没有 vhost 更改的情况下不重新启动 Apache?

How to arrange a website deployment with Ansible such that Apache is not restarted if there is no vhost change?

我目前正在学习 Ansible,为了应用我所学的知识,我正在转换一些旧的 Bash 安装脚本来构建 Web 服务器。我的一个用例是安装或升级网站。流程一般是:

如果需要虚拟主机,我只对重新启动网络服务器感兴趣,主要是因为我的 SSL 证书上有密码,如果发生这种情况,则需要重新输入。由于此剧本的大部分运行都是升级而不是安装,因此在不需要的地方禁止重启是有意义的。

我已经围绕这个用例进行了一些搜索,但我似乎无法在网上找到很多相关的 material。因此,我创建了以下内容,使用文件哈希来检测更改,我想知道是否有更好的方法来做到这一点。这是:

---

# Copy site contents unconditionally
- file: path=/var/www/html state=directory
- copy: src=../../build-files/default/index.html dest=/var/www/html/index.html

# Copy vhost to a temporary file so we can checksum it remotely
- copy: src=../../build-files/apache/000-default.conf dest=/tmp/000-default.conf

# Get the checksum of the existing vhost
- shell: md5sum /etc/apache2/sites-available/000-default.conf | cut -f 1 -d ' '
  register: old_checksum_default_site

# Get the checksum of the new vhost
- shell: md5sum /tmp/000-default.conf | cut -f 1 -d ' '
  register: new_checksum_default_site

- debug: msg="Old checksum is {{ old_checksum_default_site.stdout }}, new checksum is {{ new_checksum_default_site.stdout }}"

# Copy our default vhost into place if necessary
- copy: src=../../build-files/apache/000-default.conf dest=/etc/apache2/sites-available/000-default.conf
  notify: restart apache
  when: old_checksum_default_site.stdout != new_checksum_default_site.stdout

对于所有站点来说,这是相当多的样板文件,而且它甚至还没有符号链接 - 有更短的方法吗?我不是 Python 程序员,但如果编写自定义模块可能是最佳解决方案,请告诉我。

在不了解 apache/vhost 东西的内部结构的情况下,这应该足够了

# Copy site contents unconditionally
- file: path=/var/www/html state=directory
- copy: src=../../build-files/default/index.html dest=/var/www/html/index.html

# Copy vhost to a temporary file so we can checksum it remotely
- copy: src=../../build-files/apache/000-default.conf dest=/etc/apache2/sites-available/000-default.conf
  notify:
    - restart apache

如果您 000-default.conf 的本地副本没有任何变化,那么当您 运行 它时,ansible 步骤将被报告为绿色。

如果发生了变化,颜色将变为黄色,您将在末尾看到处理程序 运行。