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
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
args:
apply:
@ -44,5 +49,6 @@
loop: "{{ systemd_services_result.results }}"
loop_control:
loop_var: services_results
label: "{{ loop_label | to_json }}"
tags:
- systemd-service

View File

@ -13,15 +13,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: "Restart socket"
- name: "Restarting socket"
systemd:
name: "{{ socket.socket_name | replace(' ', '_') }}.socket"
state: restarted
when:
- "'socket_name' in socket"
- socket.restart_changed | default(systemd_service_restart_changed) | bool
- "'state' not in socket"
- socket.enabled | default(systemd_service_enabled) | bool
loop: "{{ socket_results.item }}"
loop: "{{ socket_results.item | selectattr('socket_name', 'defined') }}"
loop_control:
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
# limitations under the License.
- name: "Restart service {{ services_results.item.service_name | replace(' ', '_') }}"
- name: "Restarting service"
systemd:
name: >-
{{ services_results.item.service_name | replace(' ', '_') }}{{ template_argument }}.{{
(services_results.item.timer is defined) | ternary('timer', 'service')
}}
name: "{{ _systemd_service_name }}{{ template_argument }}.{{ _systemd_service_type }}"
state: restarted
loop: "{{ services_results.item.template_arguments | default(['']) }}"
loop_control:
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) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
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:
- systemd-service
@ -47,7 +54,14 @@
owner: "{{ item.systemd_user_name | default(systemd_user_name) }}"
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
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:
- systemd-service
@ -59,7 +73,14 @@
group: "{{ item.systemd_group_name | default(systemd_group_name) }}"
mode: "02755"
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:
- systemd-service
@ -67,10 +88,13 @@
template:
src: "systemd-tmpfiles.j2"
dest: "/etc/tmpfiles.d/{{ item.systemd_tempd_prefix | default(systemd_tempd_prefix) }}-{{ item.service_name | replace(' ', '_') }}.conf"
mode: "0644"
owner: "root"
group: "root"
with_items: "{{ systemd_services }}"
loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
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:
- systemd-service
@ -78,12 +102,17 @@
openstack.config_template.config_template:
src: "systemd-service.j2"
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_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)
notify:
- systemd service changed
@ -95,10 +124,13 @@
template:
src: "systemd-service-overrides.j2"
dest: "/etc/systemd/system/{{ item.service_name | replace(' ', '_') }}.service.d/override.conf"
mode: "0644"
owner: "root"
group: "root"
with_items: "{{ systemd_services }}"
loop: "{{ systemd_services }}"
loop_control:
label: "{{ loop_label | to_json }}"
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"
notify:
- systemd service changed
@ -115,7 +147,13 @@
group: "root"
when:
- 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:
- systemd service changed
register: systemd_timer_result
@ -130,6 +168,12 @@
owner: "root"
group: "root"
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:
- systemd socket changed
- systemd service changed
@ -148,9 +192,14 @@
- name: Including systemd_load tasks
include_tasks: systemd_load.yml
vars:
_systemd_service_name: "{{ service.service_name | replace(' ', '_') }}"
loop_label:
service_name: "{{ _systemd_service_name }}"
loop: "{{ systemd_services }}"
loop_control:
loop_var: service
label: "{{ loop_label | to_json }}"
args:
apply:
tags:
@ -164,5 +213,12 @@
enabled: "{{ item.enabled | default(systemd_service_enabled) }}"
state: "{{ item.state | default(omit) }}"
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:
- systemd-service

View File

@ -16,9 +16,9 @@
# 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
# we reduce code duplication
- name: "Load service {{ service.service_name | replace(' ', '_') }}"
- name: "Load service {{ _systemd_service_name }}"
systemd:
name: "{{ service.service_name | replace(' ', '_') }}{{ template_argument }}.service"
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))) }}"
when:
@ -26,10 +26,16 @@
loop: "{{ service.template_arguments | default(['']) }}"
loop_control:
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:
name: "{{ service.service_name | replace(' ', '_') }}{{ template_argument }}.timer"
name: "{{ _systemd_service_name }}{{ template_argument }}.timer"
enabled: "{{ service.enabled | default(systemd_service_enabled) }}"
state: "{{ service.timer.state | default(omit) }}"
when:
@ -37,3 +43,9 @@
with_items: "{{ service.template_arguments | default(['']) }}"
loop_control:
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') }}"