Reduce output by leveraging loop labels

Systemd service element does contain quite bug dictionary and
output of it is not really useful for most tasks. To make output smaller
and at the same time more informative we leverage loop labels.

With that we convert all with_items to be proper loops.

Change-Id: I90d2fb4110d2a47af184e90382538e015eb87b13
This commit is contained in:
Dmitriy Rabotyagov 2023-03-03 12:22:19 +01:00 committed by Dmitriy Rabotyagov
parent b763b73ffc
commit 4e68dde47a
5 changed files with 110 additions and 28 deletions

View File

@ -31,6 +31,11 @@
- name: Restart changed services - name: Restart changed services
include_tasks: handlers/systemd_restart.yml include_tasks: handlers/systemd_restart.yml
vars:
_systemd_service_name: "{{ services_results.item.service_name | replace(' ', '_') }}"
_systemd_service_type: "{{ (services_results.item.timer is defined) | ternary('timer', 'service') }}"
loop_label:
service_name: "{{ _systemd_service_name }}.{{ _systemd_service_type }}"
listen: systemd service changed listen: systemd service changed
args: args:
apply: apply:
@ -44,5 +49,6 @@
loop: "{{ systemd_services_result.results }}" loop: "{{ systemd_services_result.results }}"
loop_control: loop_control:
loop_var: services_results loop_var: services_results
label: "{{ loop_label | to_json }}"
tags: tags:
- systemd-service - systemd-service

View File

@ -13,15 +13,19 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
- name: "Restart socket" - name: "Restarting socket"
systemd: systemd:
name: "{{ socket.socket_name | replace(' ', '_') }}.socket" name: "{{ socket.socket_name | replace(' ', '_') }}.socket"
state: restarted state: restarted
when: when:
- "'socket_name' in socket"
- socket.restart_changed | default(systemd_service_restart_changed) | bool - socket.restart_changed | default(systemd_service_restart_changed) | bool
- "'state' not in socket" - "'state' not in socket"
- socket.enabled | default(systemd_service_enabled) | bool - socket.enabled | default(systemd_service_enabled) | bool
loop: "{{ socket_results.item }}" loop: "{{ socket_results.item | selectattr('socket_name', 'defined') }}"
loop_control: loop_control:
loop_var: socket loop_var: socket
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ socket.socket_name | replace(' ', '_') }}.socket"
state: restarted

View File

@ -13,13 +13,17 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
- name: "Restart service {{ services_results.item.service_name | replace(' ', '_') }}" - name: "Restarting service"
systemd: systemd:
name: >- name: "{{ _systemd_service_name }}{{ template_argument }}.{{ _systemd_service_type }}"
{{ services_results.item.service_name | replace(' ', '_') }}{{ template_argument }}.{{
(services_results.item.timer is defined) | ternary('timer', 'service')
}}
state: restarted state: restarted
loop: "{{ services_results.item.template_arguments | default(['']) }}" loop: "{{ services_results.item.template_arguments | default(['']) }}"
loop_control: loop_control:
loop_var: template_argument loop_var: template_argument
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ _systemd_service_name }}"
state: restarted
template: "{{ template_argument }}"
type: "{{ _systemd_service_type }}"

View File

@ -36,7 +36,14 @@
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}" owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}" group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755" mode: "02755"
with_items: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "{{ item.systemd_run_dir | default(systemd_run_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
tags: tags:
- systemd-service - systemd-service
@ -47,7 +54,14 @@
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}" owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}" group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755" mode: "02755"
with_items: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "{{ item.systemd_lock_dir | default(systemd_lock_dir) }}/{{ item.systemd_slice_name | default(systemd_slice_name) | replace(' ', '_') }}"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
tags: tags:
- systemd-service - systemd-service
@ -59,7 +73,14 @@
group: "{{ item.systemd_group_name | default(systemd_group_name) }}" group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755" mode: "02755"
when: "'systemd_overrides' in item" when: "'systemd_overrides' in item"
with_items: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
path: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d"
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
tags: tags:
- systemd-service - systemd-service
@ -67,10 +88,13 @@
template: template:
src: "systemd-tmpfiles.j2" src: "systemd-tmpfiles.j2"
dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf" dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf"
mode: "0644" loop: "{{ systemd_services }}"
owner: "root" loop_control:
group: "root" label: "{{ loop_label | to_json }}"
with_items: "{{ systemd_services }}" vars:
loop_label:
src: "systemd-tmpfiles.j2"
dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf"
tags: tags:
- systemd-service - systemd-service
@ -78,12 +102,17 @@
openstack.config_template.config_template: openstack.config_template.config_template:
src: "systemd-service.j2" src: "systemd-service.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service" dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service"
mode: "0644"
owner: "root"
group: "root"
config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}" config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}"
config_type: "ini" config_type: "ini"
with_items: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-service.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service"
config_overrides: "{{ item.config_overrides | default(systemd_service_config_overrides) }}"
config_type: "ini"
when: not (item.systemd_overrides_only | default(False) | bool) when: not (item.systemd_overrides_only | default(False) | bool)
notify: notify:
- systemd service changed - systemd service changed
@ -95,10 +124,13 @@
template: template:
src: "systemd-service-overrides.j2" src: "systemd-service-overrides.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf" dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf"
mode: "0644" loop: "{{ systemd_services }}"
owner: "root" loop_control:
group: "root" label: "{{ loop_label | to_json }}"
with_items: "{{ systemd_services }}" vars:
loop_label:
src: "systemd-service-overrides.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf"
when: "'systemd_overrides' in item" when: "'systemd_overrides' in item"
notify: notify:
- systemd service changed - systemd service changed
@ -115,7 +147,13 @@
group: "root" group: "root"
when: when:
- item.timer is defined - item.timer is defined
with_items: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-timer.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.timer"
notify: notify:
- systemd service changed - systemd service changed
register: systemd_timer_result register: systemd_timer_result
@ -130,6 +168,12 @@
owner: "root" owner: "root"
group: "root" group: "root"
loop: "{{ systemd_services | subelements('sockets', skip_missing=True) }}" loop: "{{ systemd_services | subelements('sockets', skip_missing=True) }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
src: "systemd-socket.j2"
dest: "/etc/systemd/system/{{ item.1.socket_name | replace(' ', '_') }}.socket"
notify: notify:
- systemd socket changed - systemd socket changed
- systemd service changed - systemd service changed
@ -148,9 +192,14 @@
- name: Including systemd_load tasks - name: Including systemd_load tasks
include_tasks: systemd_load.yml include_tasks: systemd_load.yml
vars:
_systemd_service_name: "{{ service.service_name | replace(' ', '_') }}"
loop_label:
service_name: "{{ _systemd_service_name }}"
loop: "{{ systemd_services }}" loop: "{{ systemd_services }}"
loop_control: loop_control:
loop_var: service loop_var: service
label: "{{ loop_label | to_json }}"
args: args:
apply: apply:
tags: tags:
@ -164,5 +213,12 @@
enabled: "{{ item.enabled | default(systemd_service_enabled) }}" enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}" state: "{{ item.state | default(omit) }}"
loop: "{{ systemd_services | selectattr('sockets', 'defined') | map(attribute='sockets') | flatten(1) }}" loop: "{{ systemd_services | selectattr('sockets', 'defined') | map(attribute='sockets') | flatten(1) }}"
loop_control:
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ item.socket_name | replace(' ', '_') }}.socket"
enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}"
tags: tags:
- systemd-service - systemd-service

View File

@ -16,9 +16,9 @@
# NOTE(noonedeadpunk): If we don't have template_arguments set, we just iterate over list with # NOTE(noonedeadpunk): If we don't have template_arguments set, we just iterate over list with
# empty string as element. This won't have any effect on the results, but # empty string as element. This won't have any effect on the results, but
# we reduce code duplication # we reduce code duplication
- name: "Load service {{ service.service_name | replace(' ', '_') }}" - name: "Load service {{ _systemd_service_name }}"
systemd: systemd:
name: "{{ service.service_name | replace(' ', '_') }}{{ template_argument }}.service" name: "{{ _systemd_service_name }}{{ template_argument }}.service"
enabled: "{{ service.enabled | default(systemd_service_enabled) }}" enabled: "{{ service.enabled | default(systemd_service_enabled) }}"
state: "{{ (service.timer is defined) | ternary(omit, (service.state | default(omit))) }}" state: "{{ (service.timer is defined) | ternary(omit, (service.state | default(omit))) }}"
when: when:
@ -26,10 +26,16 @@
loop: "{{ service.template_arguments | default(['']) }}" loop: "{{ service.template_arguments | default(['']) }}"
loop_control: loop_control:
loop_var: template_argument loop_var: template_argument
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ _systemd_service_name }}{{ template_argument }}.service"
enabled: "{{ service.enabled | default(systemd_service_enabled) }}"
state: "{{ (service.timer is defined) | ternary('omit', (service.state | default('omit'))) }}"
- name: "Load timer {{ service.service_name | replace(' ', '_') }}" - name: "Load timer {{ _systemd_service_name }}"
systemd: systemd:
name: "{{ service.service_name | replace(' ', '_') }}{{ template_argument }}.timer" name: "{{ _systemd_service_name }}{{ template_argument }}.timer"
enabled: "{{ service.enabled | default(systemd_service_enabled) }}" enabled: "{{ service.enabled | default(systemd_service_enabled) }}"
state: "{{ service.timer.state | default(omit) }}" state: "{{ service.timer.state | default(omit) }}"
when: when:
@ -37,3 +43,9 @@
with_items: "{{ service.template_arguments | default(['']) }}" with_items: "{{ service.template_arguments | default(['']) }}"
loop_control: loop_control:
loop_var: template_argument loop_var: template_argument
label: "{{ loop_label | to_json }}"
vars:
loop_label:
name: "{{ _systemd_service_name }}{{ template_argument }}.timer"
enabled: "{{ service.enabled | default(systemd_service_enabled) }}"
state: "{{ service.timer.state | default('omit') }}"