Skip to content

Commit 53444cd

Browse files
committed
fix: do not mix facts with vars
Cause: The variables __podman_user and __podman_user_home_dir were being set by both `vars` and `set_fact`. This causes unpredictable and undefined behavior. Consequence: When managing resources for two different users, the variables __podman_user and __podman_user_home_dir were using the old values from the first user, so config files for the first user were being used for the second user. Fix: Ensure that __podman_user is only ever set with `set_fact`, and __podman_user_home_dir is only ever set with `vars`. Refactor the code to use __podman_handle_user instead of __podman_user where a `vars` could be used. Result: Data for multiple users is kept separate. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent 4e9d0b4 commit 53444cd

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

tasks/handle_certs_d.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
- name: Check user and group information
2020
include_tasks: handle_user_group.yml
2121
vars:
22+
__podman_handle_user: "{{ __podman_user }}"
2223
__podman_spec_item: "{{ __podman_cert_spec_item }}"
2324

2425
- name: Set per-cert spec variables part 2
25-
set_fact:
26-
__podman_user_home_dir: "{{
27-
ansible_facts['getent_passwd'][__podman_user][4] }}"
28-
29-
- name: Set per-cert spec variables part 3
3026
set_fact:
3127
__podman_certs_d_path: "{{ (__podman_user_home_dir ~
3228
__podman_user_certs_d_path
3329
if __podman_rootless else __podman_system_certs_d_path) ~
3430
'/' ~ __podman_cert_spec_item['registry_host'] }}"
31+
vars:
32+
__podman_user_home_dir: "{{
33+
ansible_facts['getent_passwd'][__podman_user][4] }}"
3534

36-
- name: Set per-cert spec variables part 4
35+
- name: Set per-cert spec variables part 3
3736
set_fact:
3837
__podman_cert_file_list:
3938
- dest: "{{ __podman_certs_d_path ~ '/' ~

tasks/handle_credential_files.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- name: Check user and group information
1212
include_tasks: handle_user_group.yml
1313
vars:
14-
__podman_user: "{{ __podman_credential_user }}"
14+
__podman_handle_user: "{{ __podman_credential_user }}"
1515
__podman_spec_item: "{{ __podman_credential_item }}"
1616

1717
- name: Set credential variables

tasks/handle_kube_spec.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
- name: Check user and group information
6767
include_tasks: handle_user_group.yml
6868
vars:
69+
__podman_handle_user: "{{ __podman_user }}"
6970
__podman_spec_item: "{{ __podman_kube_spec_item }}"
7071

7172
- name: Fail if no kube spec is given
@@ -81,8 +82,6 @@
8182
set_fact:
8283
__podman_xdg_runtime_dir: >-
8384
/run/user/{{ ansible_facts["getent_passwd"][__podman_user][1] }}
84-
__podman_user_home_dir: "{{
85-
ansible_facts['getent_passwd'][__podman_user][4] }}"
8685
__podman_systemd_scope: "{{ __podman_systemd_unit_scope
8786
if __podman_systemd_unit_scope
8887
and __podman_systemd_unit_scope | length > 0
@@ -96,6 +95,9 @@
9695
__podman_kube_path: "{{ __podman_user_home_dir ~
9796
__podman_user_kube_path
9897
if __podman_rootless else __podman_system_kube_path }}"
98+
vars:
99+
__podman_user_home_dir: "{{
100+
ansible_facts['getent_passwd'][__podman_user][4] }}"
99101

100102
- name: Set per-container variables part 5
101103
set_fact:

tasks/handle_quadlet_spec.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,13 @@
9797
- name: Check user and group information
9898
include_tasks: handle_user_group.yml
9999
vars:
100+
__podman_handle_user: "{{ __podman_user }}"
100101
__podman_spec_item: "{{ __podman_quadlet_spec_item }}"
101102

102103
- name: Set per-container variables part 3
103104
set_fact:
104105
__podman_xdg_runtime_dir: >-
105106
/run/user/{{ ansible_facts["getent_passwd"][__podman_user][1] }}
106-
__podman_user_home_dir: "{{
107-
ansible_facts['getent_passwd'][__podman_user][4] }}"
108107
__podman_systemd_scope: "{{ __podman_systemd_unit_scope
109108
if __podman_systemd_unit_scope
110109
and __podman_systemd_unit_scope | length > 0
@@ -140,6 +139,9 @@
140139
__podman_quadlet_path: "{{ __podman_user_home_dir ~
141140
__podman_user_quadlet_path
142141
if __podman_rootless else __podman_system_quadlet_path }}"
142+
vars:
143+
__podman_user_home_dir: "{{
144+
ansible_facts['getent_passwd'][__podman_user][4] }}"
143145

144146
- name: Get kube yaml contents
145147
slurp:

tasks/handle_secret.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- name: Check user and group information
99
include_tasks: handle_user_group.yml
1010
vars:
11+
__podman_handle_user: "{{ __podman_user }}"
1112
__podman_spec_item: "{{ __podman_secret_item }}"
1213
__podman_check_subids: false
1314
no_log: true

tasks/handle_user_group.yml

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
# Inputs:
2+
# __podman_handle_user: string - name of user
3+
# __podman_spec_item: dict - object with more information
4+
# Outputs:
5+
# ansible_facts["getent_passwd"][__podman_handle_user]
6+
# __podman_group
7+
# podman_subuid_info
8+
# podman_subgid_info
19
---
210
- name: Get user information
311
getent:
412
database: passwd
5-
key: "{{ __podman_user }}"
13+
key: "{{ __podman_handle_user }}"
614
fail_key: false
715
when: "'getent_passwd' not in ansible_facts or
8-
__podman_user not in ansible_facts['getent_passwd']"
16+
__podman_handle_user not in ansible_facts['getent_passwd']"
917

1018
- name: Fail if user does not exist
1119
fail:
1220
msg: >
13-
The given podman user [{{ __podman_user }}] does not exist -
21+
The given podman user [{{ __podman_handle_user }}] does not exist -
1422
cannot continue
15-
when: not ansible_facts["getent_passwd"][__podman_user]
23+
when: not ansible_facts["getent_passwd"][__podman_handle_user]
1624

1725
- name: Set group for podman user
1826
set_fact:
@@ -23,7 +31,7 @@
2331
{%- elif podman_run_as_group is not none -%}
2432
{{ podman_run_as_group }}
2533
{%- else -%}
26-
{{ ansible_facts["getent_passwd"][__podman_user][2] }}
34+
{{ ansible_facts["getent_passwd"][__podman_handle_user][2] }}
2735
{%- endif -%}
2836
2937
- name: Check subids
@@ -37,27 +45,27 @@
3745
# does not work for root
3846
- name: Use getsubids if available
3947
when:
40-
- __podman_user not in ["root", "0"]
48+
- __podman_handle_user not in ["root", "0"]
4149
- __podman_stat_getsubids.stat.exists
4250
block:
4351
- name: Check with getsubids for user subuids
44-
command: getsubids {{ __podman_user | quote }}
52+
command: getsubids {{ __podman_handle_user | quote }}
4553
changed_when: false
4654
register: __podman_register_subuids
4755

4856
- name: Check with getsubids for user subgids
49-
command: getsubids -g {{ __podman_user | quote }}
57+
command: getsubids -g {{ __podman_handle_user | quote }}
5058
changed_when: false
5159
register: __podman_register_subgids
5260

5361
- name: Set user subuid and subgid info
5462
set_fact:
5563
podman_subuid_info: "{{ podman_subuid_info | d({}) |
56-
combine({__podman_user:
64+
combine({__podman_handle_user:
5765
{'start': __subuid_data[2] | int, 'range': __subuid_data[3] | int}})
5866
if __subuid_data | length > 0 else podman_subuid_info | d({}) }}"
5967
podman_subgid_info: "{{ podman_subgid_info | d({}) |
60-
combine({__podman_user:
68+
combine({__podman_handle_user:
6169
{'start': __subgid_data[2] | int, 'range': __subgid_data[3] | int}})
6270
if __subgid_data | length > 0 else podman_subgid_info | d({}) }}"
6371
vars:
@@ -67,7 +75,7 @@
6775
- name: Check subuid, subgid files if no getsubids
6876
when:
6977
- not __podman_stat_getsubids.stat.exists
70-
- __podman_user not in ["root", "0"]
78+
- __podman_handle_user not in ["root", "0"]
7179
block:
7280
- name: Get subuid file
7381
slurp:
@@ -82,35 +90,35 @@
8290
- name: Set user subuid and subgid info
8391
set_fact:
8492
podman_subuid_info: "{{ podman_subuid_info | d({}) |
85-
combine({__podman_user:
93+
combine({__podman_handle_user:
8694
{'start': __subuid_data[1] | int, 'range': __subuid_data[2] | int}})
8795
if __subuid_data else podman_subuid_info | d({}) }}"
8896
podman_subgid_info: "{{ podman_subgid_info | d({}) |
89-
combine({__podman_user:
97+
combine({__podman_handle_user:
9098
{'start': __subgid_data[1] | int, 'range': __subgid_data[2] | int}})
9199
if __subgid_data else podman_subgid_info | d({}) }}"
92100
vars:
93101
__subuid_match_line: "{{
94102
(__podman_register_subuids.content | b64decode).split('\n') | list |
95-
select('match', '^' ~ __podman_user ~ ':') | list }}"
103+
select('match', '^' ~ __podman_handle_user ~ ':') | list }}"
96104
__subuid_data: "{{ __subuid_match_line[0].split(':') | list
97105
if __subuid_match_line else none }}"
98106
__subgid_match_line: "{{
99107
(__podman_register_subgids.content | b64decode).split('\n') | list |
100-
select('match', '^' ~ __podman_user ~ ':') | list }}"
108+
select('match', '^' ~ __podman_handle_user ~ ':') | list }}"
101109
__subgid_data: "{{ __subgid_match_line[0].split(':') | list
102110
if __subgid_match_line else none }}"
103111

104112
- name: Fail if user not in subuid file
105113
fail:
106114
msg: >
107-
The given podman user [{{ __podman_user }}] is not in the
115+
The given podman user [{{ __podman_handle_user }}] is not in the
108116
/etc/subuid file - cannot continue
109-
when: not __podman_user in podman_subuid_info
117+
when: not __podman_handle_user in podman_subuid_info
110118

111119
- name: Fail if user not in subgid file
112120
fail:
113121
msg: >
114-
The given podman user [{{ __podman_user }}] is not in the
122+
The given podman user [{{ __podman_handle_user }}] is not in the
115123
/etc/subgid file - cannot continue
116-
when: not __podman_user in podman_subgid_info
124+
when: not __podman_handle_user in podman_subgid_info

tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
- name: Check user and group information
123123
include_tasks: handle_user_group.yml
124124
vars:
125-
__podman_user: "{{ podman_run_as_user }}"
125+
__podman_handle_user: "{{ podman_run_as_user }}"
126126
__podman_spec_item: {}
127127

128128
- name: Set config file paths

0 commit comments

Comments
 (0)