SegaffAB
SegaffAB

Reputation: 1

Fetch a file from task in same Ansible playbook

How do I transfer a file I have created from a previous task in my Ansible playbook? Here is what I got so far:

- name: Create Yum Report
  shell: |
     cd /tmp
     yum history info > $(hostname -s)_$(date "+%d-%m-%Y").txt
  register: after_pir

- name: Transfer PIR
  fetch:
     src: /tmp/{{ after_pir }}
     dest: /tmp/

However, I receive this error message when I run my playbook.

TASK [Transfer PIR] ************************************************************************************************************
failed: [x.x.x.x] (item=after_pir) => {"ansible_loop_var": "item", "changed": false, "item": "after_pir", "msg": "the remote file does not exist, not transferring, ignored"}

I have tried to run different fetch, synchronzie and pull methods but I'm not sure what the issue is.

Upvotes: 0

Views: 358

Answers (1)

Zeitounator
Zeitounator

Reputation: 44645

One way to do that:

    - name: Create Yum Report
      command: yum history info
      register: yum_report

    - name: Dump report on local disk for each host
      copy:
         content: "{{ yum_report.stdout }}"
         dest: "/tmp/{{ inventory_hostname_short }}-{{ '%d-%m-%Y' | strftime }}"
      delegate_to: localhost

Upvotes: 2

Related Questions