Ansible – How to Show Diff of Newly Created File in Check Mode

ansibleansible-playbook

Let's say I manage file myfile.conf with Ansible, e.g.:

- template:
    src: "myfile.conf.j2"
    dest: "/etc/myfile.conf"

I can then see the diff in a dry-run, like this:

$ ansible-playbook --check --diff myplaybook.yaml

Cool. But it seems to only display the diff when the file exists on the remote host. If the file is absent, I don't see the output other than task OK.

How do I tell Ansible to show the diff if the file is absent currently?

Perhaps similar to how Git would show a diff in a commit of a newly created file in style like with /dev/null as base in the diff:

--- /dev/null
+++ b/myfile.conf.j2

Best Answer

Works for me (ansible 2.7.9):

  - name: Create haproxy.cfg
    template:
      src: templates/haproxy.cfg.j2
      dest: /var/tmp/haproxy-keepalived/haproxy.cfg

ansible-playbook --check --diff haproxy-keepalived.yaml
...
changed: [kmaster2]
--- before
+++ after: /root/.ansible/tmp/ansible-local-31057pak66sgj/tmp_de6cqgb/haproxy.cfg.j2
@@ -0,0 +1,33 @@
+global
+  daemon
+  log 127.0.0.1 local0 debug
...

Perhaps it is version-dependent or you are hitting max_diff_size (default 104448 bytes).