0

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

For example: In the directory /home/user1/test/ there is always only one file named test1 or some other name. I need to extract the filename (test1) from this path to some variable

- 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"

Then I've should get "test1" as variable and use it in the new filename in this code:

      copy:
        src: /home/myuser/myfile
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 0666
      with_items:
        - { dest: '/home/user2/test/{{ files_matched }}' }

As result of first script i've got:

: 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}]

But i need only test1 part as result, not this all. Thank you!

Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
Deema
  • 1
  • 3
  • If you are sure that In the directory /home/user1/test/ will always have only one file, then use this content: "{{ files_matched.files[0].path }}" . this will provide you the full path of the file as output (/home/user2/test/test1). – Ada Pongaya Jul 31 '19 at 07:07
  • Thank you! Thats working! And then how can I get (test1) output from (/home/user1/test/test1)? – Deema Jul 31 '19 at 07:24

2 Answers2

0

Q: "How can I take filename from a specific directory and use it as a variable?'

A: Given the tree

$ tree /home/user1/test/
/home/user1/test/
├── test1
├── test2
└── test3

the tasks below

- find:
    paths: /home/user1/test/
  register: files_matched
- debug:
    msg: "{{ '/home/user2/test/' ~ item|basename }}"
  loop: "{{ files_matched.files|json_query('[*].path') }}

give

"msg": "/home/user2/test/test1"
"msg": "/home/user2/test/test3"
"msg": "/home/user2/test/test2"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

I hope you are expecting something like this. this will save the list of files under the specified directory into the /tmp/find_result.txt. I hope from here you can proceed.

---
- name: find file
  hosts: linux
  tasks:
  - name: Ansible find file examples
    find:
     paths: /home/user1/test/
    register: files_matched

  - name: "Save find results to file"
    lineinfile:
      line: "{{ item.path }}"
      path: "/tmp/find_result.txt"
      create: yes
    loop: "{{ files_matched.files | flatten }}"

note: make sure that you delete the /tmp/find_result.txt once your task completed.

if you want to store only the filename not the entire path, then replace line: "{{ item.path }}" with line: "{{ item.path | basename }}"

Ada Pongaya
  • 405
  • 4
  • 18