I am new to Ansible & trying to figure out this thing of splitting up directory path so that I can store the new path in new variable & use it later. I am dropping the draft of my playbook below.
---
- name: new directory path
hosts: servers
become: true
become_user: root
gather_facts: true
tasks:
- name: directory path1
file:
path: /home/usr/Desktop/hello/hello1
register: path1
- name: splitting path
file:
path: path1.split('/')
register: path2
What I want is to extract: /home/usr/Desktop/hello from /home/usr/Desktop/hello/hello1 and store it in a new variable path2 so that I can use path2 later. Please suggest what shall I follow?
You can use dirname filter to get a directory from a path. Below code snippet will set /home/usr/Desktop/hello
to dir
.
Note, do not add a trailing /
to the given path otherwise ansible will consider that the directory and will return the same path.
- set_fact:
dir: "{{ '/home/usr/Desktop/hello/hello1' | dirname }}"