|
| 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:: |
0 commit comments