Skip to content

Commit e720d24

Browse files
authored
Folder creation for volumes (containers) (#55)
* Added folder creation for volumes (containers, not pods) First it checks if the folder already exists and if it does, it won't adjust any permissions. This helps if podman can't manage the permissions correctly. It allows for changing the owner and group in case it is needed to set a specific UID and GID. It also allows to change the mode. I added explanations for :U as well, which tells podman to change the permissions to the container user recuresively. This works if the service inside the container doesn't run with a different user than the container.
1 parent b54df90 commit e720d24

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ What role does:
1919
and restarts container if image changed (not for pod yet)
2020
* creates systemd file for container or pod
2121
* creates kubernetes yaml for pod
22+
* creates volume directories for containers if they do not exist. (for pod use DirectoryOrCreate)
2223
* set's container or pod to be always automatically restarted if container dies.
2324
* makes container or pod enter run state at system boot
2425
* adds or removes containers exposed ports to firewall.
2526
* It takes parameter for running rootless containers under given user
26-
(I didn't test this with pod mode yet)
2727

2828
For reference, see these two blogs about the role:
2929
* [Automate Podman Containers with Ansible 1/2](https://redhatnordicssa.github.io/ansible-podman-containers-1)
@@ -72,8 +72,16 @@ note that some options apply only to other method.
7272
- ```container_cmd_args``` - Any command and arguments passed to podman-run after specifying the image name. Not used for pod.
7373
- ```container_run_as_user``` - Which user should systemd run container as.
7474
Defaults to root.
75-
- ```container_run_as_group``` - Which grou should systemd run container as.
75+
- ```container_run_as_group``` - Which group should systemd run container as.
7676
Defaults to root.
77+
- ```container_dir_owner``` - Which owner should the volume dirs have.
78+
Defaults to container_run_as_user.
79+
If you use :U as a volume option podman will set the permissions for the user inside the container automatically.
80+
Quote: The :U suffix tells Podman to use the correct host UID and GID based on the UID and GID within the container, to change recursively the owner and group of the source volume. Warning use with caution since this will modify the host filesystem.
81+
- ```container_dir_group``` - Which group should the volume dirs have.
82+
Defaults to container_run_as_group.
83+
- ```container_dir_mode``` - Which permissions should the volume dirs have.
84+
Defaults to '0755'.
7785
- ```container_state``` - container is installed and run if state is
7886
```running```, and stopped and systemd file removed if ```absent```
7987
- ```container_firewall_ports``` - list of ports you have exposed from container
@@ -128,7 +136,7 @@ Root container:
128136
container_name: lighttpd
129137
container_run_args: >-
130138
--rm
131-
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z
139+
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
132140
--label "io.containers.autoupdate=image"
133141
-p 8080:80
134142
#container_state: absent
@@ -148,13 +156,6 @@ Rootless container:
148156
name: rootless_user
149157
comment: I run sample container
150158
151-
- name: ensure directory
152-
file:
153-
name: /tmp/podman-container-systemd
154-
owner: rootless_user
155-
group: rootless_user
156-
state: directory
157-
158159
- name: tests container
159160
vars:
160161
container_run_as_user: rootless_user
@@ -164,7 +165,7 @@ Rootless container:
164165
container_name: lighttpd
165166
container_run_args: >-
166167
--rm
167-
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z
168+
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
168169
-p 8080:80
169170
#container_state: absent
170171
container_state: running

tasks/create_container_volume.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
- name: Check if {{ item }} is existing
3+
become: yes
4+
ansible.builtin.stat:
5+
path: "{{ item }}"
6+
register: _container_folder
7+
8+
- name: Create directory {{ item }} and set permissions
9+
become: yes
10+
ansible.builtin.file:
11+
path: "{{ item }}"
12+
owner: "{{ container_dir_owner|default(container_run_as_user) }}"
13+
group: "{{ container_dir_group|default(container_run_as_group) }}"
14+
mode: '{{ container_dir_mode|default(omit) }}'
15+
state: directory
16+
when: not (_container_folder.stat.isdir is defined and _container_folder.stat.isdir)

tasks/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@
187187
- container_run_as_user != "root"
188188
- not user_lingering.stat.exists
189189

190+
- name: "Ensure volume directories exist for {{ container_name }}"
191+
ansible.builtin.include_tasks: create_container_volume.yml
192+
loop: "{{ container_run_args | regex_findall('-v ([^:]*)') }}"
193+
when:
194+
- container_image_list is defined and container_image_list | length == 1
195+
- container_run_args is defined and container_run_args | length > 0
196+
- container_pod_yaml is undefined
197+
190198
- name: "create systemd service file for container: {{ container_name }}"
191199
template:
192200
src: systemd-service-single.j2

0 commit comments

Comments
 (0)