大网数据广告活动
服务器运维问答

How do I let Ansible show the diff of a newly created file in check mode?

· 52CE 我爱测

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 se…

问题原文

原帖链接 · gertvdijk · CC BY-SA 4.0

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

已采纳解答

原帖链接 · Mark Wagner · CC BY-SA 4.0

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).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。