Skip to content

Commit

Permalink
Ansible local fact management strategy change
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 5, 2024
1 parent 84edec4 commit 56b475e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 81 deletions.
43 changes: 3 additions & 40 deletions roles/ansible/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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']
29 changes: 29 additions & 0 deletions roles/ansible/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions roles/ansible/tasks/assign_local_facts.yml

This file was deleted.

2 changes: 1 addition & 1 deletion roles/ansible/tasks/local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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({}) }}"
ansible__updated_local_facts: "{{ ansible_local[ansible__local_fact_name] | default({}) }}"
5 changes: 0 additions & 5 deletions roles/ansible/tasks/set_facts.yml

This file was deleted.

31 changes: 1 addition & 30 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
38 changes: 38 additions & 0 deletions roles/ansible/vars/main.yml
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit 56b475e

Please sign in to comment.