From 2d2562d7de0917e7c260e725f20405dd5eafaa3d Mon Sep 17 00:00:00 2001 From: Murat Aybars <39916128+aybarsm@users.noreply.github.com> Date: Sat, 6 Jul 2024 17:17:17 +0100 Subject: [PATCH] PRE - Role: ansible - Local fact management optimisation #2 --- roles/ansible/handlers/main.yml | 13 +----- roles/ansible/tasks/main.yml | 16 +++---- roles/ansible/tasks/update_local_facts.yml | 43 ++++++------------- .../templates/aybarsm_linux.json.fact.j2 | 8 +++- 4 files changed, 26 insertions(+), 54 deletions(-) diff --git a/roles/ansible/handlers/main.yml b/roles/ansible/handlers/main.yml index a324709..73b314f 100644 --- a/roles/ansible/handlers/main.yml +++ b/roles/ansible/handlers/main.yml @@ -1,12 +1 @@ ---- -- name: Settle local facts on host if changed - become: true - ansible.builtin.template: - src: "{{ ansible__local_fact_template }}" - dest: "{{ ansible__local_fact_file }}" - backup: "{{ ansible__local_fact_backup | default(omit) | bool }}" - register: ansible__settle_local_facts - listen: "ansible__settle_local_facts" - when: - - linux_role_ansible_enabled | bool - - linux_role_ansible_manage_local_facts | bool \ No newline at end of file +--- \ No newline at end of file diff --git a/roles/ansible/tasks/main.yml b/roles/ansible/tasks/main.yml index 126bfd5..0ab058a 100644 --- a/roles/ansible/tasks/main.yml +++ b/roles/ansible/tasks/main.yml @@ -2,13 +2,9 @@ - name: Load role related variables ansible.builtin.include_vars: main.yml -# - name: Include update local facts tasks for initiation -# ansible.builtin.include_tasks: update_local_facts.yml -# vars: -# ansible__local_fact_updates: -# - path: ansible.init -# value: -# when: -# - linux_role_ansible_enabled | bool -# - linux_role_ansible_manage_local_facts | bool -# - ansible_local[ansible__local_fact_name] is undefined \ No newline at end of file +- name: Include update local facts tasks for initiation + ansible.builtin.include_tasks: update_local_facts.yml + when: + - ansible__role_enabled | bool + - ansible__manage_local_facts | bool + - ansible_local[ansible__local_fact_name] is undefined \ No newline at end of file diff --git a/roles/ansible/tasks/update_local_facts.yml b/roles/ansible/tasks/update_local_facts.yml index e20d8ba..c2e2618 100644 --- a/roles/ansible/tasks/update_local_facts.yml +++ b/roles/ansible/tasks/update_local_facts.yml @@ -9,53 +9,35 @@ when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool - - ansible__local_fact_updates | default([]) | length > 0 - -# - name: Temporarily assign host local facts to ansible facts -# ansible.builtin.set_fact: -# __ansible__local_facts: "{{ ansible_local[ansible__local_fact_name] | default({}) }}" -# register: ansible__assign_local_facts -# when: -# - ansible__role_enabled | bool -# - ansible__manage_local_facts | bool -# - ansible__local_fact_updates | default([]) | length > 0 - name: Update host local facts ansible.utils.update_fact: updates: "{{ local_fact_updates }}" vars: - # update_paths: "{{ ansible__local_fact_updates | map(attribute='path') | - # map('regex_replace', '^ansible_local\\.' + ansible__local_fact_name + '\\.|^ansible_local\\.', '') | - # map('regex_replace', '^(?!__ansible__local_facts\\.)(.*)$', '__ansible__local_facts.\\1') }}" path_prefix: "ansible_local.{{ ansible__local_fact_name }}." update_paths: "{{ ansible__local_fact_updates | map(attribute='path') | map('regex_replace', '^(?!' + (path_prefix | regex_escape) + ')(.*)$', path_prefix + '\\1') }}" local_fact_updates: "{{ {'path': update_paths, 'value': (ansible__local_fact_updates | map(attribute='value'))} | aybarsm.helper.to_list_of_dicts }}" - register: ansible__update_local_facts - notify: "ansible__settle_local_facts" + register: ansible__update_local_facts when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool - ansible__local_fact_updates | default([]) | length > 0 + - ansible_local[ansible__local_fact_name] is defined -# - name: Undefine host local facts in ansible facts -# ansible.builtin.set_fact: -# __ansible__local_facts: "{{ undef() }}" -# register: ansible__undefine_local_facts -# when: -# - ansible__role_enabled | bool -# - ansible__manage_local_facts | bool -# - ansible__local_fact_updates | default([]) | length > 0 - -- name: Settle local facts on the host if they have been modified - ansible.builtin.meta: 'flush_handlers' +- name: Settle updated local facts on the host + become: true + ansible.builtin.template: + src: "{{ ansible__local_fact_template }}" + dest: "{{ ansible__local_fact_file }}" + backup: "{{ ansible__local_fact_backup | default(omit) | bool }}" + register: ansible__settle_local_facts when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool - - ansible__local_fact_updates | default([]) | length > 0 -- name: Re-read local facts on the host +- name: Re-read updated local facts on the host become: true ansible.builtin.setup: filter: ansible_local @@ -63,6 +45,5 @@ when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool - - ansible__local_fact_updates | default([]) | length > 0 - - ansible__settle_local_facts is defined - - ansible__settle_local_facts.changed \ No newline at end of file + - ansible__settle_local_facts.changed is defined + - ansible__settle_local_facts.changed | bool \ No newline at end of file diff --git a/roles/ansible/templates/aybarsm_linux.json.fact.j2 b/roles/ansible/templates/aybarsm_linux.json.fact.j2 index 63ba846..08e6073 100644 --- a/roles/ansible/templates/aybarsm_linux.json.fact.j2 +++ b/roles/ansible/templates/aybarsm_linux.json.fact.j2 @@ -1,5 +1,11 @@ +{% if ansible_local[ansible__local_fact_name] is defined and ansible__update_local_facts is defined %} {% set facts_ansible_settled = ansible_local[ansible__local_fact_name] | default({}) %} {% set facts_role_update_settled = ansible__update_local_facts.ansible_local[ansible__local_fact_name] | default({}) %} {% set list_merge_strategy = ansible__local_fact_list_merge_strategy | default('prepend') %} {% set final_local_facts = facts_ansible_settled | combine(facts_role_update_settled, recursive=true, list_merge=list_merge_strategy) %} -{{ final_local_facts | to_nice_json }} \ No newline at end of file +{{ final_local_facts | to_nice_json }} +{% else %} +{ + "init": "{{ now().utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') }}" +} +{% endif %} \ No newline at end of file