如果目录的内容已被修改,我如何在ansible中运行任务

时间:2017-02-05 11:25:15

标签: ansible yaml

我正在尝试为我的playbook编写一个任务,只有在对我的wordpress目录进行更改时才运行后续代码。我想出了下面的脚本,但是当对wordpress目录进行更改时,重新部署wordpress应用程序的代码并没有运行。

- name: Run if content is added or deleted in application directory
  shell: find /var/www/html/wordpress -type f -exec md5sum {} \; | sort -k 2 | md5sum
  register: change

- name: Copy code to application directory
unarchive:
  src:  /root/wordpress.zip
  dest: /var/www/html
  owner: apache
  group: apache
  mode: 0644
when:
  - change.stdout|success
notify:
  - Reload Apache server

1 个答案:

答案 0 :(得分:0)

我认为问题在于您正在使用shell模块。 shell模块在Ansible中被视为级别模块,这意味着它没有" Idempotence "烤了。

默认情况下,每次运行shell命令都会运行。

我处理它的方式是我为我的应用程序使用 git module ,然后git模块告诉我我的git repo的状态是否已更改与我的制作中的应用程序状态有关。

示例:

- name: Ensure demo application is at correct release.
        git:
           repo: https://github.com/foobar
           version: "{{ app_version }}"
           dest: "{{ app_directory }}"
           force: true
           accept_hostkey: true
        register: app_updated
        notify: restart nginx

在此示例中,只会发生更改(您需要的操作),更改git repo的状态(与服务器上的repo相关)以及我的playbook中的所有相关操作都是将基于这一事实。

因此,您已走上正轨,但我建议您使用 copy 模块,该模块仅在文件或文件夹之间发生更改时才会复制文件或文件夹您的源目录和远程目录。

然后你可以用一个变量捕捉到这个变化(就像你做的那样),然后在你的剧本中使用它。

相关问题