Ansible - Get directory name using the dirname filter

There are two similar filters, basename and dirname.

For example, with "/tmp/foo.txt", the dirname will be "/tmp" and the basename will be "foo.txt".

---
- hosts: localhost
  tasks:
  - name: print dirname
    debug:
      msg: "{{ '/tmp/foo.txt' | dirname }}"

  - name: print basename
    debug:
      msg: "{{ '/tmp/foo.txt' | basename }}"
...

 

This can also be used with map.

---
- hosts: localhost
  tasks:
  - name: set_fact dirname
    set_fact:
      dirname: "{{ '/tmp/foo.txt' | map('dirname') }}"

  - name: set_fact basename
    set_fact:
      basename: "{{ '/tmp/foo.txt' | map('basename') }}"
...

 

The following should be returned.

TASK [print dirname] 
ok: [localhost] => {
    "msg": "/tmp"
}

TASK [print basename] 
ok: [localhost] => {
    "msg": "foo.txt"
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter e7461 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |