我目前正在学习Ansible,为了应用我所学的内容,我正在转换一些旧的Bash安装脚本来构建web服务器。我的一个用例是安装或升级一个网站。过程一般是:
- 将Apache vhost定义复制到
/etc/apache2/available-sites
/etc/apache2/enabled-sites
中的Symlink vhost定义- 签出
/var/www/sitename
中的分支或从源文件夹复制 - 在项目内部运行自定义设置或迁移脚本,例如使用Phing
- Apache的优雅重启
如果需要vhost,我只想重新启动web服务器,主要是因为我的SSL证书上有一个密码短语,如果发生这种情况,需要重新输入。由于该剧本的大多数运行都是升级而非安装,因此在不需要重新启动的地方取消重新启动是有意义的。
我已经围绕这个用例进行了一些搜索,但在网上似乎找不到太多相关的材料。因此,我创建了以下内容,使用文件哈希来检测更改,我想知道是否有更好的方法
---
# 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
的本地副本没有更改,则在运行时,可执行的步骤将报告为绿色。
如果发生了更改,那么颜色将为黄色,您将看到处理程序在最后运行。