Find an entry in a file and email the file if the entry IS NOT found - Tower

13 views
Skip to first unread message

scott.f...@wyndhamdn.com

unread,
Mar 19, 2018, 7:43:44 AM3/19/18
to Ansible Project
Hi, I am fairly new to Ansible, so would appreciate some help.

I am trying to look in a .csv or .txt file.  I need to email it when the entry '0 rows' does not appear.  Basically the file has been produced by a software vendor, and a result of '0 rows' confirms no errors.  Any other result would mean there are errors.

I have tried the following code, after not having much success with 'lineinfile'.  However, the play emails me whether it has found the text or not.  I did try a debug argument on it to view the output and noted that 'json' was full off characters similar to:

"presence.stdout": "ÿþJ\u0000o\u0000b\u0000 \u0000'\u0000P\u0000a\u0000u\u0000s\u0000e\u0000 \u0000a\u0000n\u0000d\u0000

However, the 'standard out' does contain the information I require:

(0 rows(s) affected)

Any suggestions are much appreciated.  Thanks.


- hosts: localhost

  tasks:

 

- name: read the fileout file

  shell: cat /directory/directory/fileout.txt

  register: presence

- name: a task that only happens if the entry exists

  when: "'0 rows' not in presence"

  mail:

      host: localhost

      port: 25

      subject: "Ansible Task. Checking for retained information Completed"

      body: "This email is confirmation that this task completed as designed"

      from: emailaddress

      to: emailaddress

      attach: /directory/directory/fileout.txt

      charset: utf8

  ignore_errors: true





Kai Stian Olstad

unread,
Mar 19, 2018, 8:26:14 AM3/19/18
to ansible...@googlegroups.com
On 19.03.2018 12:43, scott.f...@wyndhamdn.com wrote:
> Hi, I am fairly new to Ansible, so would appreciate some help.
>
> I am trying to look in a .csv or .txt file. I need to email it when
> the
> entry '0 rows' does not appear. Basically the file has been produced
> by a
> software vendor, and a result of '0 rows' confirms no errors. Any
> other
> result would mean there are errors.
>
> I have tried the following code, after not having much success with
> 'lineinfile'. However, the play emails me whether it has found the
> text or
> not.

lineinfile is used to add line(s) to a file.


> I did try a debug argument on it to view the output and noted that
> 'json' was full off characters similar to:
>
> "presence.stdout": "ÿþJ\u0000o\u0000b\u0000
> \u0000'\u0000P\u0000a\u0000u\u0000s\u0000e\u0000
> \u0000a\u0000n\u0000d\u0000
>
> However, the 'standard out' does contain the information I require:
>
> (0 rows(s) affected)
>
> Any suggestions are much appreciated. Thanks.
>
>
> - hosts: localhost
>
> tasks:
>
> - name: read the fileout file
>
> shell: cat /directory/directory/fileout.txt
>
> register: presence
>
> - name: a task that only happens if the entry exists
>
> when: "'0 rows' not in presence"

So you information is in presence.stdout but you are using presence
instead.

presence is a dictionary that contain a lot of information about the
shell task, the output of the command in the shell is stored in
presence.stdout so you need to use that one.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages