Fetch remote files, add them to local file

18 views
Skip to first unread message

John Harmon

unread,
Nov 2, 2017, 1:44:10 PM11/2/17
to Ansible Project
I have the following playbook but have 2 problems:

  1.  The contents of the file are garbled.  Why is that?  How can I clear it up?
  2.  How can I just grab the contents of the file only?  {{file_contents.results.content}} complains

---
- hosts: "{{ host }}"
  tasks
:
   
- name: Fetch files from remote server
      slurp
:
        src
: "{{item.src}}"
      with_items
:
       
- {src: "/etc/security/limits.conf"}
       
- {src: "/etc/sysctl.conf"}
       
- {src: "/boot/grub/grub.conf"}
     
register: file_contents

#    - name: DEBUG
#      debug:
#        var: file_contents

   
- name: Write results
      lineinfile
:
        dest
: "/tmp/hugepages_results.txt"
        line
: "HOST: {{ansible_nodename}} \n{{file_contents.results}}\n"
        insertafter
: EOF
        create
: yes
        state
: present
      delegate_to
: localhost



John Harmon

unread,
Nov 2, 2017, 3:55:10 PM11/2/17
to Ansible Project
I ended up going this route:

---
- hosts: "{{ host }}"
  vars
:
    dest_dir
: "/tmp/fetched"
  tasks
:
   
- name: Ensure destination directory exists
      file
:
        dest
: "{{ dest_dir }}"
        state
: directory
      changed_when
: false


   
- name: Fetch files from
remote server
      fetch
:
        src
: "{{item.src}}"
        dest
: "{{item.dest}}"
        flat
: yes
      with_items
:
       
- {src: "/etc/security/limits.conf", dest: "{{ dest_dir }}/{{ inventory_hostname }}_limits.conf"}
       
- {src: "/etc/sysctl.conf", dest: "{{ dest_dir }}/{{ inventory_hostname }}_sysctl.conf"}
       
- {src: "/boot/grub/grub.conf", dest: "{{ dest_dir }}/{{ inventory_hostname }}_grub.conf"}

   
- name: Compress results
      archive
:
        path
: "{{ dest_dir }}/"
        dest
: "{{ dest_dir }}.tar.bz2"
        format
: bz2
      delegate_to
: localhost
      notify
:
       
- Email results
       
- Cleanup local files



Reply all
Reply to author
Forward
0 new messages