Reputation: 1783
I am trying to copy the output from the linux find command to a flat file without losing the line-breaks and I can't seem to find a way to do it. the contents of the file are always without line breaks. Any ideas would be greatly appreciated.
My code:
- name: Play to run find command and capture its output to a file
hosts: all
tasks:
- name: 'Run find command to fetch file rights {{inventory_hostname}}'
command: 'find /var/log/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"'
register: find_results
become: true
become_user: root
become_method: sudo
- name: Print to verify it works
debug:
msg: '{{find_results.stdout}}'
- name: Use copy module to create the file using output from the previous command.
copy:
dest: "/tmp/find_results.txt"
content: >
"{{ find_results.stdout_lines | list }}"
delegate_to: localhost
### This doesn't work either
# - name: Use blockinfile to do the same
# blockinfile:
# path: "/tmp/find_results.txt"
# block: |
# "{{ find_results.stdout_lines }}"
My output:
$ cat /tmp/find_results.txt
"[u'svrt1,644,/var/log/rhsm/rhsm.log-20210221;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210228;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210307;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210325;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log;', u'svrt1,644,/var/log/rhsm/rhsm.log;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210221;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210228;', u'svrt1,644,/var/log/rhsm/rhsmcertd.log-20210307;', u'svrt1,644,/var/log/rhsm/rhsm.log-20210325;']"
expected output:
$ cat /tmp/find_results.txt
svrt1,644,/var/log/rhsm/rhsm.log-20210221;
svrt1,644,/var/log/rhsm/rhsm.log-20210228;
svrt1,644,/var/log/rhsm/rhsm.log-20210307;
svrt1,644,/var/log/rhsm/rhsmcertd.log-20210325;
svrt1,644,/var/log/rhsm/rhsmcertd.log;
svrt1,644,/var/log/rhsm/rhsm.log;
svrt1,644,/var/log/rhsm/rhsmcertd.log-20210221;
svrt1,644,/var/log/rhsm/rhsmcertd.log-20210228;
svrt1,644,/var/log/rhsm/rhsmcertd.log-20210307;
svrt1,644,/var/log/rhsm/rhsm.log-20210325;
Upvotes: 0
Views: 1762
Reputation: 124
- name: Play to run find command and capture its output to a file
hosts: localhost
connection: local
tasks:
- name: 'Run find command to fetch file rights {{inventory_hostname}}'
command: 'find /var/tmp/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"'
register: find_results
become: true
become_user: root
become_method: sudo
- name: Print to verify it works
debug:
msg: '{{find_results.stdout}}'
- name: Use copy module to create the file using output from the previous command.
copy:
dest: "/tmp/find_results.txt"
content: "{{ item }}"
with_items: "{{ find_results.stdout }}"
delegate_to: localhost
[rohtash@172 blockinfile]$ vi find.yml
[rohtash@172 blockinfile]$ ansible-playbook find.yml
PLAY [Play to run find command and capture its output to a file] ******************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]
TASK [Run find command to fetch file rights localhost] ****************************************************************************************************************************************
changed: [localhost]
TASK [Print to verify it works] ***************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "localhost,664,/var/tmp/rhsm/abc1;\nlocalhost,664,/var/tmp/rhsm/abc2;\nlocalhost,664,/var/tmp/rhsm/abc3;\nlocalhost,664,/var/tmp/rhsm/abc4;\nlocalhost,664,/var/tmp/rhsm/abc5;\nlocalhost,664,/var/tmp/rhsm/abc6;"
}
TASK [Use copy module to create the file using output from the previous command.] *************************************************************************************************************
changed: [localhost] => (item=localhost,664,/var/tmp/rhsm/abc1;
localhost,664,/var/tmp/rhsm/abc2;
localhost,664,/var/tmp/rhsm/abc3;
localhost,664,/var/tmp/rhsm/abc4;
localhost,664,/var/tmp/rhsm/abc5;
localhost,664,/var/tmp/rhsm/abc6;)
PLAY RECAP ************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[rohtash@172 blockinfile]$ cat /tmp/find_results.txt
localhost,664,/var/tmp/rhsm/abc1;
localhost,664,/var/tmp/rhsm/abc2;
localhost,664,/var/tmp/rhsm/abc3;
localhost,664,/var/tmp/rhsm/abc4;
localhost,664,/var/tmp/rhsm/abc5;
localhost,664,/var/tmp/rhsm/abc6;[rohtash@172 blockinfile]$
OR
dont use pipe after blockinfile. Use only find_results. stdout not stdout_lines like this.
[rohtash@172 blockinfile]$ ansible-playbook find_withblock.yml
PLAY [Play to run find command and capture its output to a file] ******************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [localhost]
TASK [Run find command to fetch file rights localhost] ****************************************************************************************************************************************
changed: [localhost]
TASK [Print to verify it works] ***************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "localhost,664,/var/tmp/rhsm/abc1;\nlocalhost,664,/var/tmp/rhsm/abc2;\nlocalhost,664,/var/tmp/rhsm/abc3;\nlocalhost,664,/var/tmp/rhsm/abc4;\nlocalhost,664,/var/tmp/rhsm/abc5;\nlocalhost,664,/var/tmp/rhsm/abc6;"
}
TASK [Use blockinfile to do the same] *********************************************************************************************************************************************************
changed: [localhost]
PLAY RECAP ************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[rohtash@172 blockinfile]$ cat /tmp/find_results_usingblocks.txt
# BEGIN ANSIBLE MANAGED BLOCK
localhost,664,/var/tmp/rhsm/abc1;
localhost,664,/var/tmp/rhsm/abc2;
localhost,664,/var/tmp/rhsm/abc3;
localhost,664,/var/tmp/rhsm/abc4;
localhost,664,/var/tmp/rhsm/abc5;
localhost,664,/var/tmp/rhsm/abc6;
# END ANSIBLE MANAGED BLOCK
[rohtash@172 blockinfile]$ cat find_withblock.yml
- name: Play to run find command and capture its output to a file
hosts: localhost
connection: local
tasks:
- name: 'Run find command to fetch file rights {{inventory_hostname}}'
command: 'find /var/tmp/rhsm -type f -printf "{{ inventory_hostname }},%m,%p;\n"'
register: find_results
become: true
become_user: root
become_method: sudo
- name: Print to verify it works
debug:
msg: '{{find_results.stdout}}'
- name: Use blockinfile to do the same
blockinfile:
path: "/tmp/find_results_usingblocks.txt"
block: "{{ find_results.stdout }}"
state: present
delegate_to: localhost
use whichever suits u best.
Upvotes: 2