Skip to content

Commit

Permalink
Role: ansible - Local fact management optimisation completed
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 6, 2024
1 parent 2d2562d commit a0fcd02
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
3 changes: 2 additions & 1 deletion roles/ansible/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

9 changes: 1 addition & 8 deletions roles/ansible/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
ansible.builtin.include_vars: main.yml
25 changes: 9 additions & 16 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,26 @@
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
ansible.builtin.template:
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
Expand Down
12 changes: 1 addition & 11 deletions roles/ansible/templates/aybarsm_linux.json.fact.j2
Original file line number Diff line number Diff line change
@@ -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 %}
{{ host_local_facts | to_nice_json }}

0 comments on commit a0fcd02

Please sign in to comment.