From 56b475e5d5923ef91dba243c9d566db85ac93e99 Mon Sep 17 00:00:00 2001 From: Murat Aybars <39916128+aybarsm@users.noreply.github.com> Date: Fri, 5 Jul 2024 19:49:11 +0100 Subject: [PATCH] Ansible local fact management strategy change --- roles/ansible/defaults/main.yml | 43 ++-------------------- roles/ansible/handlers/main.yml | 29 +++++++++++++++ roles/ansible/tasks/assign_local_facts.yml | 5 --- roles/ansible/tasks/local_facts.yml | 2 +- roles/ansible/tasks/set_facts.yml | 5 --- roles/ansible/tasks/update_local_facts.yml | 31 +--------------- roles/ansible/vars/main.yml | 38 +++++++++++++++++++ 7 files changed, 72 insertions(+), 81 deletions(-) delete mode 100644 roles/ansible/tasks/assign_local_facts.yml delete mode 100644 roles/ansible/tasks/set_facts.yml create mode 100644 roles/ansible/vars/main.yml diff --git a/roles/ansible/defaults/main.yml b/roles/ansible/defaults/main.yml index 2d5661d..03d5b22 100644 --- a/roles/ansible/defaults/main.yml +++ b/roles/ansible/defaults/main.yml @@ -1,46 +1,9 @@ ansible__role_enabled: true ansible__manage_local_facts: true -ansible__local_fact_template: aybarsm_linux.json.fact.j2 ansible__local_fact_backup: true ansible__local_facts_dir: "{{ (not lookup('config', 'DEFAULT_FACT_PATH')) | ternary('/etc/ansible/facts.d', lookup('config', 'DEFAULT_FACT_PATH')) }}" -ansible__local_fact_file: "{{ ansible__local_facts_dir }}/aybarsm_linux.fact" +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__config: - modules: - ansible_builtin_apt: - uniques: ['name'] - aliases: - allow_downgrade: ['allow-downgrade', 'allow_downgrades', 'allow-downgrades'] - allow_unauthenticated: ['allow-unauthenticated'] - default_release: ['default-release'] - install_recommends: ['install-recommends'] - name: ['package', 'pkg'] - update_cache: ['update-cache'] - ansible_builtin_apt_repository: - uniques: ['repo'] - aliases: - update_cache: ['update-cache'] - ansible_builtin_apt_key: - uniques: ['keyserver', 'url', 'id', 'file'] - ansible_builtin_dnf: - uniques: ['name'] - aliases: - name: ['package', 'pkg'] - update_cache: ['expire-cache'] - ansible_builtin_yum_repository: - uniques: ['name'] - aliases: - attributes: ['attr'] - sslcacert: ['ca_cert'] - sslclientcert: ['client_cert'] - sslclientkey: ['client_key'] - sslverify: ['validate_certs'] - ansible_builtin_copy: - uniques: ['dest'] - aliases: - attributes: ['attr'] - ansible_builtin_template: - uniques: ['dest'] - aliases: - attributes: ['attr'] \ No newline at end of file diff --git a/roles/ansible/handlers/main.yml b/roles/ansible/handlers/main.yml index 8203b70..2ab404a 100644 --- a/roles/ansible/handlers/main.yml +++ b/roles/ansible/handlers/main.yml @@ -1,7 +1,36 @@ --- +# 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" + - 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" + when: + - ansible__local_fact_name is defined + - ansible_local[ansible__local_fact_name] is defined \ No newline at end of file diff --git a/roles/ansible/tasks/assign_local_facts.yml b/roles/ansible/tasks/assign_local_facts.yml deleted file mode 100644 index a1956cd..0000000 --- a/roles/ansible/tasks/assign_local_facts.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- name: Assign host local facts to ansible facts - ansible.builtin.set_fact: - __ansible__local_facts: "{{ ansible__updated_local_facts }}" - register: ansible__local_facts_assign \ No newline at end of file diff --git a/roles/ansible/tasks/local_facts.yml b/roles/ansible/tasks/local_facts.yml index fe1819e..5589524 100644 --- a/roles/ansible/tasks/local_facts.yml +++ b/roles/ansible/tasks/local_facts.yml @@ -11,4 +11,4 @@ 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 + ansible__updated_local_facts: "{{ ansible_local[ansible__local_fact_name] | default({}) }}" \ No newline at end of file diff --git a/roles/ansible/tasks/set_facts.yml b/roles/ansible/tasks/set_facts.yml deleted file mode 100644 index a4a4454..0000000 --- a/roles/ansible/tasks/set_facts.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -- name: Set facts for ansible role - ansible.builtin.set_fact: - __ansible__local_fact_name: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}" - register: ansible__set_facts \ 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 30a484c..46528f6 100644 --- a/roles/ansible/tasks/update_local_facts.yml +++ b/roles/ansible/tasks/update_local_facts.yml @@ -4,41 +4,12 @@ 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\\.' + ansible__local_fact_name + '\\.|^ansible_local\\.', '') | 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 - -- name: Import assign host local facts tasks for re-assignment - ansible.builtin.import_tasks: - file: assign_local_facts.yml - vars: - ansible__updated_local_facts: "{{ ansible__update_local_facts.__ansible__local_facts }}" - -- 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 notify: "ansible__local_facts_reread" - register: ansible__local_facts_settle - -# - name: Re-read local facts from the host -# become: true -# ansible.builtin.setup: -# filter: ansible_local -# register: ansible__host_facts_reread -# when: ansible__host_facts_settle.changed - name: Re-read local facts on the host if they have been modified ansible.builtin.meta: 'flush_handlers' \ No newline at end of file diff --git a/roles/ansible/vars/main.yml b/roles/ansible/vars/main.yml new file mode 100644 index 0000000..46744a8 --- /dev/null +++ b/roles/ansible/vars/main.yml @@ -0,0 +1,38 @@ +__ansible__config: + modules: + ansible_builtin_apt: + uniques: ['name'] + aliases: + allow_downgrade: ['allow-downgrade', 'allow_downgrades', 'allow-downgrades'] + allow_unauthenticated: ['allow-unauthenticated'] + default_release: ['default-release'] + install_recommends: ['install-recommends'] + name: ['package', 'pkg'] + update_cache: ['update-cache'] + ansible_builtin_apt_repository: + uniques: ['repo'] + aliases: + update_cache: ['update-cache'] + ansible_builtin_apt_key: + uniques: ['keyserver', 'url', 'id', 'file'] + ansible_builtin_dnf: + uniques: ['name'] + aliases: + name: ['package', 'pkg'] + update_cache: ['expire-cache'] + ansible_builtin_yum_repository: + uniques: ['name'] + aliases: + attributes: ['attr'] + sslcacert: ['ca_cert'] + sslclientcert: ['client_cert'] + sslclientkey: ['client_key'] + sslverify: ['validate_certs'] + ansible_builtin_copy: + uniques: ['dest'] + aliases: + attributes: ['attr'] + ansible_builtin_template: + uniques: ['dest'] + aliases: + attributes: ['attr'] \ No newline at end of file