仅当目录的内容已被修改时,如何在ansible中运行任务

仅当目录的内容已被修改时,如何在ansible中运行任务,ansible,yaml,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

我正试图为我的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

我认为问题在于您使用的是shell模块。 shell模块在Ansible中被视为一个级别模块,这意味着它没有嵌入“幂等性”

因此,默认情况下,shell命令将在每次运行时运行

我处理它的方式是,我为我的应用程序使用一个,然后git模块告诉我,我的git回购的状态是否相对于我生产的应用程序的状态发生了变化

例如:

- 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)时,才会发生变化(您想要的动作),我的剧本中的所有相关动作都将基于这个事实

因此,您的思路是正确的,但我建议您使用模块,该模块仅在源目录和远程目录之间发生更改时才会复制文件或文件夹

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