cjnash
cjnash

Reputation: 1248

Ansible - Writing debug to a file: 'dict object' has no attribute 'stdout_lines'

I am currently trying to diff a bunch of files, and then record the differences between files that have been changed. I started by referencing this post: Compare two files with Ansible and omitting the last part of the task like so:

- name: Get cksum of the first file
  stat:
    path: "baselinedFiles/{{ inventory_hostname }}/file"
    checksum_algorithm: sha1
    get_checksum: yes
  register: myfristfile
  delegate_to: localhost

- name: Current SHA1 p1
  set_fact:
    mf1sha1: "{{ myfristfile.stat.checksum }}"
  delegate_to: localhost

- name: Get cksum of second file
  stat:
    path: "tmpFiles/{{ inventory_hostname }}/file"
    checksum_algorithm: sha1
    get_checksum: yes
  register: mysecondfile
  delegate_to: localhost

- name: Current SHA1 p2
  set_fact:
    mf2sha1: "{{ mysecondfile.stat.checksum }}"
  delegate_to: localhost

The above simply gets the SHA1 so we can easily compare them in the future when I am writing the diff to the files (I only want to write the diff if the files are different). This works as intended.

Next, I followed difference in two file using ansible module where I am using check_mode and diff to show the differences between the files I am checking.

- name: "Show diff if the hashes are different"
  copy:
    src: "baselinedFiles/{{ inventory_hostname }}/file"
    dest: "tmpFiles/{{ inventory_hostname }}/file"
  check_mode: yes
  diff: yes
  register: diffOutput
  delegate_to: localhost

This works and gives an output like so:

TASK [diffNlog : Show diff if the hashes are different] *******************************************************************************************************************
ok: [a -> localhost]
ok: [b -> localhost]
ok: [c -> localhost]
ok: [d -> localhost]
--- before: tmpFiles/e/file
+++ after: /home/me/someDir/baselinedFiles/e/file
@@ -78,5 +78,4 @@
 #
 # End of: blah
 #
-#######################
-#THIS IS A CHANGE THAT MIGHT HAVE BEEN MADE
+#######################
\ No newline at end of file

changed: [e -> localhost]
ok: [f -> localhost]

This is good as it shows that there was a change and what it was, which is what I want. Unfortunately, the problems arise within my next step, where I am literally just trying to get the above output that I pasted and put it into a file so it can be easily viewed. I attempt to do that this way:

- local_action: shell echo "{{ diffOutput }}" > logs/mytestlog
  when: mf1sha1 != mf2sha1
  delegate_to: localhost
  become: true
  become_user: me

This is where I am running into logic issues. It completes fine but it throws the ENTIRETY of files 1 and 2 into the log file and then shows the changes. This is way too much unnecessary output that I don't want. I then tried this solution: Ansible playbook shell output where I use debug, but unfortunately I keep on getting this error:

"The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'

as it seems that the variable that I saved from the output from diff doesn't have the stdout_lines attribute. As I said previously, I would like to just log the output that ansible displays to your console while it is running your playbook, but I am stuck here.

Any help is greatly appreciated. Thanks

Upvotes: 1

Views: 1917

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68254

It is possible to use the difference of the file before and after

- set_fact:
    my_diff: "{{ diffOutput.diff.0.before|list|
                 difference(diffOutput.diff.0.after|list) }}"

To get diffOutput.stdout_lines use command

- command: "diff {{ file1 }} {{ file2 }}"
  register: diffOutput
  ignore_errors: yes

Upvotes: 1

Related Questions