Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions tasks/get_update_user_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,11 @@
- name: Update systemd user info with new user info
when: __systemd_user_name not in __systemd_user_info
set_fact:
__systemd_user_info: |
{% set getent = ansible_facts["getent_passwd"][__systemd_user_name] %}
{% set rv = __systemd_user_info | d({}) %}
{% if __systemd_user_name not in rv %}
{% set _ = rv.__setitem__(__systemd_user_name, {}) %}
{% endif %}
{% if "xdg_dir" not in rv[__systemd_user_name] %}
{% set xdg_dir = "/run/user/" ~ getent[1] %}
{% set _ = rv[__systemd_user_name].update({"xdg_dir": xdg_dir}) %}
{% endif %}
{% if "units_dir" not in rv[__systemd_user_name] %}
{% if __systemd_user_name == "root" %}
{% set _ = rv[__systemd_user_name].update({"units_dir": __admin_units_dir}) %}
{% else %}
{% set units_dir = getent[4] ~ '/' ~ __user_units_dir %}
{% set _ = rv[__systemd_user_name].update({"units_dir": units_dir}) %}
{% endif %}
{% endif %}
{% if "group" not in rv[__systemd_user_name] %}
{% set group = getent[2] %}
{% set _ = rv[__systemd_user_name].update({"group": group}) %}
{% endif %}
{{ rv }}
__systemd_user_info: "{{ __systemd_user_info | combine({__systemd_user_name: user_dict}) }}"
vars:
getent: "{{ ansible_facts['getent_passwd'][__systemd_user_name] }}"
user_dict:
xdg_dir: /run/user/{{ getent[1] }}
units_dir: "{{ __admin_units_dir if __systemd_user_name == 'root'
else getent[4] ~ '/' ~ __user_units_dir }}"
group: "{{ getent[2] }}"
64 changes: 30 additions & 34 deletions tasks/manage_user_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,39 @@
loop_control:
loop_var: __systemd_user_name

- name: Reset __systemd_dict_list
set_fact:
__systemd_dict_list: []

# convert to the output format
- name: Convert to list of dict with user data
set_fact:
__systemd_dict_list: |
{% set rv = [] %}
{% for item in __systemd_list %}
{% set newitem = {} %}
{% if item is mapping %}
{% set _ = newitem.update(item) %}
{% else %}
{% set _ = newitem.update({"item": item}) %}
{% endif %}
{% if "state" not in newitem %}
{% set _ = newitem.update({"state": "present"}) %}
{% endif %}
{% if "user" not in newitem %}
{% set _ = newitem.update({"user": "root"}) %}
{% endif %}
{% set user = newitem["user"] %}
{% if "group" not in newitem %}
{% set _ = newitem.update({"group": __systemd_user_info[user]["group"]}) %}
{% endif %}
{% if "xdg_dir" not in newitem %}
{% set _ = newitem.update({"xdg_dir": __systemd_user_info[user]["xdg_dir"]}) %}
{% endif %}
{% if "units_dir" not in newitem %}
{% set _ = newitem.update({"units_dir": __systemd_user_info[user]["units_dir"]}) %}
{% endif %}
{% if "mode" not in newitem %}
{% set _ = newitem.update({"mode": (user == "root") | ternary("0644", "0600")}) %}
{% endif %}
{% if "dir_mode" not in newitem %}
{% set _ = newitem.update({"dir_mode": (user == "root") | ternary("0755", "0700")}) %}
{% endif %}
{% set _ = rv.append(newitem) %}
{% endfor %}
{{ rv }}
__systemd_dict_list: "{{ __systemd_dict_list +
[newitem | combine(newstate, newuser, newgroup, newxdg_dir, newunits_dir, newmode, newdir_mode)] }}"
loop: "{{ __systemd_list }}"
vars:
newitem: "{{ item if item is mapping else {'item': item} }}"
newstate:
state: "{{ item['state'] if item is mapping and 'state' in item
else 'present' }}"
newuser:
user: "{{ item['user'] if item is mapping and 'user' in item
else 'root' }}"
newgroup:
group: "{{ item['group'] if item is mapping and 'group' in item
else __systemd_user_info[newuser['user']]['group'] }}"
newxdg_dir:
xdg_dir: "{{ item['xdg_dir'] if item is mapping and 'xdg_dir' in item
else __systemd_user_info[newuser['user']]['xdg_dir'] }}"
newunits_dir:
units_dir: "{{ item['units_dir'] if item is mapping and 'units_dir' in item
else __systemd_user_info[newuser['user']]['units_dir'] }}"
newmode:
mode: "{{ item['mode'] if item is mapping and 'mode' in item
else (newuser['user'] == 'root') | ternary('0644', '0600') }}"
newdir_mode:
dir_mode: "{{ item['dir_mode'] if item is mapping and 'dir_mode' in item
else (newuser['user'] == 'root') | ternary('0755', '0700') }}"

- name: Enable linger if needed
command: loginctl enable-linger {{ item | quote }}
Expand Down
16 changes: 8 additions & 8 deletions tests/tests_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
roles:
- linux-system-roles.systemd
tasks:
- name: Get UnitFileState=
- name: Get UnitFileState= - 2
# noqa command-instead-of-module
command: systemctl show -p UnitFileState foo.service
register: unit_file_state_now
Expand All @@ -158,13 +158,13 @@
roles:
- linux-system-roles.systemd
pre_tasks:
- name: Save UnitFileState before calling role
- name: Save UnitFileState before calling role - 2
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
register: sshd_state_before
changed_when: false
tasks:
- name: Get UnitFileState=
- name: Get UnitFileState= - 3
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
register: sshd_state_after
Expand All @@ -187,7 +187,7 @@
roles:
- linux-system-roles.systemd
tasks:
- name: Get UnitFileState=
- name: Get UnitFileState= - 4
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
register: sshd_state_now
Expand Down Expand Up @@ -277,7 +277,7 @@
systemd_masked_units:
- "{{ test_unit }}"

- name: Get test unit state
- name: Get test unit state - 2
# noqa command-instead-of-module
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
register: test_unit_state
Expand All @@ -290,7 +290,7 @@
test_unit_state.stdout is search("UnitFileState=bad")
- test_unit_state.stdout is search("SubState=dead")

- name: Ensure test unit is running and unmasked
- name: Ensure test unit is running and unmasked - 2
include_role:
name: linux-system-roles.systemd
vars:
Expand All @@ -299,13 +299,13 @@
systemd_unmasked_units:
- "{{ test_unit }}"

- name: Get test unit state
- name: Get test unit state - 3
# noqa command-instead-of-module
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
register: test_unit_state
changed_when: false

- name: Ensure test unit running and unmasked
- name: Ensure test unit running and unmasked - 2
assert:
that:
- test_unit_state.stdout is search("UnitFileState=enabled") or
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_user_units.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
vars:
systemd_disabled_units: "{{ __systemd_disabled_units }}"

- name: Get unit file state of units after
- name: Get unit file state of units after - 2
# noqa command-instead-of-module
command: systemctl {{ scope }} show -p UnitFileState {{ item.item }}
changed_when: false
Expand Down
Loading