Ansible – Fetch with Variable Filename

ansible

I'm new to Ansible, and am actually just beginning to implement it in our environment. Our organization requires us to keep a log of updates applied to our servers. For consistency and clarity these file names are created using a command like:

yum history info > $(date +"%Y%m%d")_$(hostname)_updates.txt

Since the file name changes each day the command is run, how would I feasibly call the filename using fetch in a playbook? I've tried variations of the following, including with and without quotes in src, feeding via a variable, and several other methods:

- name: Retrieve History
  fetch:
    src: '$(date +"%Y%m%d")_$(hostname)_updates.txt'
    dest: /path/to/file/
    flat: yes

I've searched and tried a number of methods but nothing has panned out and I'm running out of threads to pull. Any suggestions?

Best Answer

May be you should try to use ansible_date_time variable and render file name with jinja?

Like this:

- name: Retrieve History
  fetch:
    src: "{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}_{{ ansible_hostname }}_updates.txt"
    dest: /path/to/file/
    flat: yes