diff --git a/roles/ansible/defaults/main.yml b/roles/ansible/defaults/main.yml index 76a2dfb..4fbfac9 100644 --- a/roles/ansible/defaults/main.yml +++ b/roles/ansible/defaults/main.yml @@ -6,5 +6,6 @@ ansible__local_facts_dir: "{{ (not lookup('config', 'DEFAULT_FACT_PATH')) | tern ansible__local_fact_name: aybarsm_linux ansible__local_fact_file: "{{ ansible__local_facts_dir }}/{{ ansible__local_fact_name }}.fact" ansible__local_fact_template: "{{ ansible__local_fact_name }}.json.fact.j2" -ansible__local_fact_list_merge_strategy: prepend +ansible__local_fact_combine_recursive: true +ansible__local_fact_combine_list_merge: prepend diff --git a/roles/ansible/tasks/main.yml b/roles/ansible/tasks/main.yml index 0ab058a..e5970b8 100644 --- a/roles/ansible/tasks/main.yml +++ b/roles/ansible/tasks/main.yml @@ -1,10 +1,3 @@ --- - 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 - when: - - ansible__role_enabled | bool - - ansible__manage_local_facts | bool - - ansible_local[ansible__local_fact_name] is undefined \ No newline at end of file + ansible.builtin.include_vars: main.yml \ 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 c2e2618..baed31b 100644 --- a/roles/ansible/tasks/update_local_facts.yml +++ b/roles/ansible/tasks/update_local_facts.yml @@ -9,22 +9,8 @@ when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool - -- name: Update host local facts - ansible.utils.update_fact: - updates: "{{ local_fact_updates }}" - vars: - 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 - 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 + - ansible__local_fact_updates is defined + - ansible__local_fact_updates | type_debug == 'dict' - name: Settle updated local facts on the host become: true @@ -32,10 +18,17 @@ src: "{{ ansible__local_fact_template }}" dest: "{{ ansible__local_fact_file }}" backup: "{{ ansible__local_fact_backup | default(omit) | bool }}" + vars: + recurse_strategy: "{{ ansible__local_fact_combine_recursive | default(true) | bool }}" + list_merge_strategy: "{{ ansible__local_fact_combine_list_merge | default('prepend') }}" + host_local_facts: "{{ ansible_local[ansible__local_fact_name] | default({'init': now().utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')}) | + combine(ansible__local_fact_updates, recursive=recurse_strategy, list_merge=list_merge_strategy) }}" register: ansible__settle_local_facts when: - ansible__role_enabled | bool - ansible__manage_local_facts | bool + - ansible__local_fact_updates is defined + - ansible__local_fact_updates | type_debug == 'dict' - name: Re-read updated local facts on the host become: true diff --git a/roles/ansible/templates/aybarsm_linux.json.fact.j2 b/roles/ansible/templates/aybarsm_linux.json.fact.j2 index 08e6073..24d187e 100644 --- a/roles/ansible/templates/aybarsm_linux.json.fact.j2 +++ b/roles/ansible/templates/aybarsm_linux.json.fact.j2 @@ -1,11 +1 @@ -{% 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 }} -{% else %} -{ - "init": "{{ now().utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') }}" -} -{% endif %} \ No newline at end of file +{{ host_local_facts | to_nice_json }} \ No newline at end of file