vtr
vtr

Reputation: 11

How to capture and display filename

I used below Ansible playbook to capture the filename, but I'm facing error

task:
  - copy:
      src: /tmp/example.tar.gz
      dest: /opt/
    register: filename

  - debug: var=filename

I need to capture the filename and to be displayed on the console.

Upvotes: 1

Views: 227

Answers (1)

U880D
U880D

Reputation: 12084

According your example of copy files to remote locations the filename seems to be already known.

If you like to set host variable(s) and fact(s) you could use a construct of

- name: Set fact
  set_fact:
    FILENAME: "example.tar.gz"

- name: Show fact
  debug:
    msg: "{{ FILENAME }}"

- name: Copy file {{ FILENAME }}
  copy:
    src: /tmp/{{ FILENAME }}
    dest: /opt

If you are interested in registering the return values of the copy module you could use

- name: Copy file
  copy:
    src: /tmp/example.tar.gz
    dest: /opt
  register: result

- name: Show copied file
  debug:
    msg: "{{ result.dest }}"

Upvotes: 1

Related Questions