title: Find filename in Ansible and Save to a file
body: <p>How can I take filename from a specific directory and use it as a variable?
  Then I'll use this variable to copy a new file with variable filename</p>
  
  <p>For example: In the directory <code>/home/user1/test/</code> there is always only one file named <code>test1</code> or some other name.
  I need to extract the filename (test1) from this path to some variable</p>
  
  <p></p>
  
  <pre><code>- hosts: linux
    become: yes
    tasks:
    - name: Ansible find file examples
      find:
       paths: /home/user1/test/
      register: files_matched
  
    - debug:
        msg: "{{ files_matched.files }}"
  
    - name: "Save find results to file"
      copy:
        content: "{{ files_matched.files }}"
        dest: "/tmp/find_result.txt"
  </code></pre>
  
  <p>Then I've should get "test1" as variable and use it in the new filename in this code:</p>
  
  <pre><code>      copy:
          src: /home/myuser/myfile
          dest: "{{ item.dest }}"
          owner: root
          group: root
          mode: 0666
        with_items:
          - { dest: '/home/user2/test/{{ files_matched }}' }
  </code></pre>
  
  <p>As result of first script i've got:</p>
  
  <pre><code>: 0, "ischr": false, "wusr": true, "xoth": false, "islnk": false, "nlink": 1, "issock": false, "rgrp": true, "gr_name": "root", "path": "/home/user1/test/test1", "xusr":
  false, "atime": 1564553299.6092095, "isdir": false, "ctime": 1564553304.7172158, "isblk": false, "xgrp": false, "dev": 2050, "wgrp": false, "isfifo": false, "mode": "0644
  ", "rusr": true}]
  </code></pre>
  
  <p>But i need only <code>test1</code> part as result, not this all.
  Thank you!</p>