diff --git a/roles/ansible/handlers/main.yml b/roles/ansible/handlers/main.yml index de0d929..a324709 100644 --- a/roles/ansible/handlers/main.yml +++ b/roles/ansible/handlers/main.yml @@ -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 \ No newline at end of file + - linux_role_ansible_enabled | bool + - linux_role_ansible_manage_local_facts | bool \ No newline at end of file diff --git a/roles/ansible/tasks/local_facts.yml b/roles/ansible/tasks/local_facts.yml deleted file mode 100644 index 5589524..0000000 --- a/roles/ansible/tasks/local_facts.yml +++ /dev/null @@ -1,14 +0,0 @@ ---- -- 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 - -- name: Import assign host local facts tasks - ansible.builtin.import_tasks: - file: assign_local_facts.yml - vars: - ansible__updated_local_facts: "{{ ansible_local[ansible__local_fact_name] | default({}) }}" \ No newline at end of file diff --git a/roles/ansible/tasks/main.yml b/roles/ansible/tasks/main.yml index 01d7c2e..ed97d53 100644 --- a/roles/ansible/tasks/main.yml +++ b/roles/ansible/tasks/main.yml @@ -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 \ 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 a0ef19d..fc1d8c4 100644 --- a/roles/ansible/tasks/update_local_facts.yml +++ b/roles/ansible/tasks/update_local_facts.yml @@ -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' \ No newline at end of file +- 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 \ No newline at end of file diff --git a/roles/ansible/templates/aybarsm_linux.json.fact.j2 b/roles/ansible/templates/aybarsm_linux.json.fact.j2 index b4b802b..b8dbfc5 100644 --- a/roles/ansible/templates/aybarsm_linux.json.fact.j2 +++ b/roles/ansible/templates/aybarsm_linux.json.fact.j2 @@ -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 }} \ No newline at end of file diff --git a/roles/ansible/vars/main.yml b/roles/ansible/vars/main.yml index 46744a8..691c37f 100644 --- a/roles/ansible/vars/main.yml +++ b/roles/ansible/vars/main.yml @@ -35,4 +35,9 @@ __ansible__config: ansible_builtin_template: uniques: ['dest'] aliases: - attributes: ['attr'] \ No newline at end of file + attributes: ['attr'] + ansible_builtin_add_host: + uniques: ['name'] + aliases: + groups: ['group', 'groupname'] + name: ['host','hostname'] \ No newline at end of file