0

I want to save the result of find into the file with an Ansible play.

- name: Find / -name "postgresql"
  find:
    paths: /var/log
    patterns: 'postgresql'

The result of above will be saved into a file. Simply, the command is find / -name "postgresql" > text.txt in shell. How do I make that command in Ansible?

1

An Ansible way of doing this with the find module could be (I am using /var/log/secure as an example since I don't run postgres):

---
- hosts: all 

  tasks:
  - name: "Find file /var/log/secure"
    find:
      paths: /var/log
      patterns: secure
    register: result

  - name: "Save find results to file"
    copy:
      content: "{{ result.files }}"
      dest: "/tmp/find_result.txt"

Content of /tmp/find_result.txt:

# cat /tmp/find_result.txt 
[{"uid": 0, "woth": false, "mtime": 1535012977.8429773, "inode": 9013905, "isgid": false, "size": 6867, "wgrp": false, "isuid": false, "isreg": true, "gid": 0, "ischr": false, "wusr": true, "xoth": false, "islnk": false, "nlink": 1, "issock": false, "rgrp": false, "path": "/var/log/secure", "xusr": false, "atime": 1534925281.706685, "isdir": false, "ctime": 1535012977.8429773, "isblk": false, "xgrp": false, "dev": 64768, "roth": false, "isfifo": false, "mode": "0600", "rusr": true}]

By registering the result, and checking the return value in files, you get access to a ton of metadata that might be useful.

If you want less output, use on of the other return values as documented here.

0

You should use the Ansible shell module in this case.

Here's an example:

- name: Find / -name "postgresql"
  shell: find / -name "postgresql" > /tmp/text.txt
  • aaahh i thought, i could make it without shell, just wanted to utilize "Find" task – Nicky Puff Aug 23 '18 at 8:11
  • Where do you want to have the output file written? Your localhost or your destination host? – Tim Aug 23 '18 at 8:13
  • After save into the file, i will run scp command to transfer that file into my other server – Nicky Puff Aug 23 '18 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.