Skip to content

Commit

Permalink
Ansible local fact management template
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 6, 2024
1 parent 56b475e commit 2c141b9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions roles/ansible/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ 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

9 changes: 0 additions & 9 deletions roles/ansible/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
---
# REVIEW: Execute the handler without when control, let the task decide to notify or not
- 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 }}"
vars:
on_host: "{{ ansible_local[ansible__local_fact_name] | default({}) | b64encode }}"
on_runtime: "{{ ansible__local_facts | default({}) | b64encode }}"
ansible_callback_diy_runner_on_skipped_msg: |
skipping: [{{ inventory_hostname }}]
msg: {{ (not ansible_check_mode and on_host != on_runtime) | ternary("Host local facts not changed.", "DRY-RUN") }}
ansible_callback_diy_runner_on_skipped_msg_color: green
when: on_host != on_runtime
register: ansible__local_facts_settle
listen: "ansible__local_facts_settle"
notify: "ansible__local_facts_reread"
Expand Down
4 changes: 2 additions & 2 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
ansible.utils.update_fact:
updates: "{{ local_fact_updates }}"
vars:
update_paths: "{{ ansible__local_fact_updates | map(attribute='path') |
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') }}"
map('regex_replace', '^(?!ansible__local_facts\\.)(.*)$', 'ansible__local_facts.\\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
Expand Down
12 changes: 6 additions & 6 deletions roles/ansible/templates/aybarsm_linux.json.fact.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if __ansible__local_facts is defined %}
{{ __ansible__local_facts | to_nice_json }}
{% else %}
{
}
{% endif %}
{% set facts_ansible_settled = ansible_local[ansible__local_fact_name] | default({}) %}
{% set facts_role_settled = ansible__local_facts | default({}) %}
{% set facts_role_update_settled = ansible__update_local_facts | default({}) %}
{% set list_merge_strategy = ansible__local_fact_list_merge_strategy | default('prepend') %}
{% set final_local_facts = facts_ansible_settled | combine(facts_role_settled, facts_role_update_settled, recursive=true, list_merge=list_merge_strategy) %}
{{ final_local_facts | to_nice_json }}

0 comments on commit 2c141b9

Please sign in to comment.