Skip to content

Commit

Permalink
Ansible local fact management redesign completed
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 6, 2024
1 parent 2c141b9 commit 1bbfdc3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
23 changes: 4 additions & 19 deletions roles/ansible/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,8 @@
src: "{{ ansible__local_fact_template }}"
dest: "{{ ansible__local_fact_file }}"
backup: "{{ ansible__local_fact_backup | default(omit) | bool }}"
register: ansible__local_facts_settle
listen: "ansible__local_facts_settle"
notify: "ansible__local_facts_reread"

- name: Re-read local facts on the host
become: true
ansible.builtin.setup:
filter: ansible_local
register: ansible__local_facts_reread
listen: "ansible__local_facts_reread"
notify: ansible__local_facts_assign

- name: Assign host local facts to ansible facts
ansible.builtin.set_fact:
ansible__local_facts: "{{ ansible_local[ansible__local_fact_name] }}"
register: ansible__local_facts_assign
listen: "ansible__local_facts_assign"
register: ansible__settle_local_facts
listen: "ansible__settle_local_facts"
when:
- ansible__local_fact_name is defined
- ansible_local[ansible__local_fact_name] is defined
- linux_role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
14 changes: 0 additions & 14 deletions roles/ansible/tasks/local_facts.yml

This file was deleted.

6 changes: 0 additions & 6 deletions roles/ansible/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
---
- name: Import linux ansible local facts tasks
ansible.builtin.import_tasks:
file: local_facts.yml
when:
- ansible__role_enabled | bool
- ansible__manage_local_facts | bool
58 changes: 54 additions & 4 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
---
- name: Ensure ansible local facts directory exists on host
become: true
ansible.builtin.file:
state: directory
recurse: true
path: "{{ ansible__local_facts_dir }}"
register: ansible__ensure_local_facts_dir
when:
- linux_role_ansible_enabled | bool
- linux_role_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:
- linux_role_ansible_enabled | bool
- linux_role_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') }}"
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
notify: "ansible__local_facts_reread"
notify: "ansible__settle_local_facts"
when:
- linux_role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
- ansible__local_fact_updates | default([]) | length > 0

- name: Undefine host local facts in ansible facts
ansible.builtin.set_fact:
__ansible__local_facts: "{{ undef() }}"
register: ansible__undefine_local_facts
when:
- linux_role_ansible_enabled | bool
- linux_role_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'
when:
- linux_role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
- ansible__local_fact_updates | default([]) | length > 0

- name: Re-read local facts on the host if they have been modified
ansible.builtin.meta: 'flush_handlers'
- name: Re-read local facts on the host
become: true
ansible.builtin.setup:
filter: ansible_local
register: ansible__reread_local_facts
when:
- linux_role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
- ansible__local_fact_updates | default([]) | length > 0
- ansible__settle_local_facts is defined
- ansible__settle_local_facts.changed
3 changes: 1 addition & 2 deletions roles/ansible/templates/aybarsm_linux.json.fact.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% 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) %}
{% 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 }}
7 changes: 6 additions & 1 deletion roles/ansible/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ __ansible__config:
ansible_builtin_template:
uniques: ['dest']
aliases:
attributes: ['attr']
attributes: ['attr']
ansible_builtin_add_host:
uniques: ['name']
aliases:
groups: ['group', 'groupname']
name: ['host','hostname']

0 comments on commit 1bbfdc3

Please sign in to comment.