Skip to content

Commit

Permalink
PRE - Role: ansible - Local fact management optimisation #2
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 6, 2024
1 parent a94aabf commit 2d2562d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 54 deletions.
13 changes: 1 addition & 12 deletions roles/ansible/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
---
16 changes: 6 additions & 10 deletions roles/ansible/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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
43 changes: 12 additions & 31 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,41 @@
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
register: ansible__reread_local_facts
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
- ansible__settle_local_facts.changed is defined
- ansible__settle_local_facts.changed | bool
8 changes: 7 additions & 1 deletion roles/ansible/templates/aybarsm_linux.json.fact.j2
Original file line number Diff line number Diff line change
@@ -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 }}
{{ final_local_facts | to_nice_json }}
{% else %}
{
"init": "{{ now().utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') }}"
}
{% endif %}

0 comments on commit 2d2562d

Please sign in to comment.