Skip to content

Commit ebbef40

Browse files
committed
content: add blog post ansible absolute path
1 parent 4d68bba commit ebbef40

7 files changed

+101
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
uuid: ad58acaf-56b0-4bcf-9b72-d6c054fc48d4
3+
date: 2023-09-20T21:39:13Z
4+
created: 2023-09-20T20:22:35.634Z
5+
aliases:
6+
title: Getting the Absolute Path of a Remote Directory in Ansible
7+
published: true
8+
modified:
9+
description: There is no builtin way to convert a relative path to an absolute path in ansible. However we can use the readlink command for this.
10+
cover_image: https://media.tiim.ch/3c1246e4-3201-4df6-af87-6aa4ab98800e.webp
11+
cover_image_txt: (stable doodle) server room, neon, cables
12+
content_tags:
13+
- dev
14+
- ansible
15+
- linux
16+
- bash
17+
---
18+
19+
I recently had to find a way to delete a folder using Ansible that was being created by Docker. The folder had a path like `~/docker/myservice`. Since docker had created it as part of a volume, the folder did not belong to the current user. So deleting the folder using normal permissions failed.
20+
21+
Deleting with elevated permission on the command line is easy: The command `sudo rm -rf ~/docker/myservice` performs the `rm` operation as the root user. In bash, this will delete the `docker/myservice` folder in the user's home directory, but when doing the equivalent in Ansible, this won't work!
22+
23+
```yaml
24+
# This does not work!
25+
- name: Delete the folder using root permissions
26+
become: true
27+
ansible.builtin.file:
28+
path: "~/docker/myservice"
29+
state: "absent"
30+
```
31+
32+
This code will try to delete the file `/user/root/docker/myservice`, which is not what we wanted.
33+
34+
The bash version works because the shell first resolves the tilde in the argument to the current users' directory before calling the sudo command. In Ansible, we first switch to the root user and only then the tilde is resolved: this time to the home directory of the root user.
35+
36+
To circumvent this, we can manually resolve the path to an absolute path. Unfortunately, I have not found a straightforward way to do this in Ansible, however the bash command `readlink -f <path>` does exactly this. To use it in Ansible, we can use the following configuration:
37+
38+
```yaml
39+
- name: Get absolute folder path
40+
ansible.builtin.command:
41+
cmd: "readlink -f ~/docker/myservice"
42+
register: folder_abs
43+
changed_when: False
44+
45+
- name: Debug
46+
debug:
47+
msg: "{{folder_abs.stdout}}" # prints /user/tim/docker/myservice
48+
49+
- name: Delete the folder using root permissions
50+
become: true
51+
ansible.builtin.file:
52+
path: "{{folder_abs.stdout}}"
53+
state: "absent"
54+
```
55+
56+
With this Ansible script, we manually resolve the absolute path and use it to delete the folder using root permissions. If you know of an easier way to resolve to an absolute path, please let me know!
57+
58+
%%
59+
60+
## Checklist
61+
62+
- [x] Finish writing text
63+
- [x] Write description
64+
- [x] tags
65+
- [x] Thumbnail Created
66+
- [x] Resized to 1024x512
67+
- [x] Uploaded using uplog
68+
- [x] Prompt text or description added
69+
- [x] Date set to timestamp
70+
- [x] Reviewed using grammarly
71+
- [x] Transferred to website/content folder
72+
- [x] Removed heading
73+
- [x] Reread on dev site
74+
75+
## 📎 Related
76+
77+
-
78+
- [[Blog Posts]]
79+
80+
## 📇 Additional Metadata
81+
82+
- 🗂 Type:: #type/content/blog-post
83+
- 📝 Status:: #status/idea
84+
- 🔐 Visibility::
85+
- 👥 Team::
86+
- 🗨 Language: #lang/en
87+
88+
**Personal**
89+
90+
- 👍 Recommended By::
91+
- 🔮 Inspired By::
92+
- 👨‍🎓 Lecturer::
93+
- 📕 Author::

content/projects/lap-counter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid: "871ebd58-0543-4d3a-9f3d-16cd85da9bf9"
33
title: Lap Counter
44
date: 2022-03-05T21:21:10+01:00
55
modified: 2022-06-08T22:15:48+02:00
6-
section: Projects
6+
section: Projects
77
published: true
88
content_tags: ["svelte", "swim", "dev"]
99
links:

content/projects/lenex-split-sheet.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid: "a03b6eef-ffbe-428d-a559-42bd361ab88c"
33
title: Lenex Splits Sheet Creator
44
date: 2022-03-05T21:21:10+01:00
55
modified: 2022-06-08T22:15:48+02:00
6-
section: Projects
6+
section: Projects
77
published: true
88
content_tags: ["swim", "lenex", "svelte", "dev"]
99
links:

content/projects/markdown-widget.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid: "c6a11779-cf98-4983-8744-9b1effae8d7a"
33
title: Android Markdown Widget
44
date: 2023-08-02T08:59:00Z
55
modified:
6-
section: Projects
6+
section: Projects
77
published: true
88
content_tags: ["android", "java", "kotlin", "markdown", "widget", "dev"]
99
links:

content/projects/pomo.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ links:
1010
- "[pomo Github](https://github.com/Tiim/pomo)"
1111
---
1212

13-
I created pomo as a way to keep me focused for working on my masters thesis, and at the same time
13+
I created pomo as a way to keep me focused for working on my masters thesis, and at the same time
1414
allowed me to learn the rust programming language.
1515

1616
Pomo is a simple pomodoro timer. It allows you to either specify the number of repetitions (pomodori), the duration of the pomodori and the duration of the breaks, or
1717
you can stecify an end time, and let pomo calculate the durations and repetitions.
1818

19-
Pomo runs as a cli tool and stores the current state in a json file. All pomo executions excep `pomo watch` just
20-
modify this json file and terminate. The watch command displays the current pomodoro timer, optionally writes the timer to a text file,
19+
Pomo runs as a cli tool and stores the current state in a json file. All pomo executions excep `pomo watch` just
20+
modify this json file and terminate. The watch command displays the current pomodoro timer, optionally writes the timer to a text file,
2121
and watches for changes of the json file.

content/projects/teamkit.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid: "a3040709-cd2d-43cb-ab33-2061ba1ae061"
33
title: TeamKit
44
date: 2022-11-27T09:19:08Z
55
modified:
6-
section: Projects
6+
section: Projects
77
published: true
88
content_tags: ["sveltekit", "hasura", "postgres", "dev"]
99
links:

content/projects/woocommerce-order-explorer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid: "8a52a1ad-a5cf-4191-947c-db6862746816"
33
title: WooCommerce Order Explorer
44
date: 2022-03-05T21:21:10+01:00
55
modified: 2022-06-08T22:15:48+02:00
6-
section: Projects
6+
section: Projects
77
published: true
88
content_tags: ["svelte", "wordpress", "woocommerce", "dev"]
99
links:

0 commit comments

Comments
 (0)