Skip to content

Commit 56b475e

Browse files
committed
Ansible local fact management strategy change
1 parent 84edec4 commit 56b475e

File tree

7 files changed

+72
-81
lines changed

7 files changed

+72
-81
lines changed

roles/ansible/defaults/main.yml

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,9 @@
11
ansible__role_enabled: true
22
ansible__manage_local_facts: true
33

4-
ansible__local_fact_template: aybarsm_linux.json.fact.j2
54
ansible__local_fact_backup: true
65
ansible__local_facts_dir: "{{ (not lookup('config', 'DEFAULT_FACT_PATH')) | ternary('/etc/ansible/facts.d', lookup('config', 'DEFAULT_FACT_PATH')) }}"
7-
ansible__local_fact_file: "{{ ansible__local_facts_dir }}/aybarsm_linux.fact"
6+
ansible__local_fact_name: aybarsm_linux
7+
ansible__local_fact_file: "{{ ansible__local_facts_dir }}/{{ ansible__local_fact_name }}.fact"
8+
ansible__local_fact_template: "{{ ansible__local_fact_name }}.json.fact.j2"
89

9-
__ansible__config:
10-
modules:
11-
ansible_builtin_apt:
12-
uniques: ['name']
13-
aliases:
14-
allow_downgrade: ['allow-downgrade', 'allow_downgrades', 'allow-downgrades']
15-
allow_unauthenticated: ['allow-unauthenticated']
16-
default_release: ['default-release']
17-
install_recommends: ['install-recommends']
18-
name: ['package', 'pkg']
19-
update_cache: ['update-cache']
20-
ansible_builtin_apt_repository:
21-
uniques: ['repo']
22-
aliases:
23-
update_cache: ['update-cache']
24-
ansible_builtin_apt_key:
25-
uniques: ['keyserver', 'url', 'id', 'file']
26-
ansible_builtin_dnf:
27-
uniques: ['name']
28-
aliases:
29-
name: ['package', 'pkg']
30-
update_cache: ['expire-cache']
31-
ansible_builtin_yum_repository:
32-
uniques: ['name']
33-
aliases:
34-
attributes: ['attr']
35-
sslcacert: ['ca_cert']
36-
sslclientcert: ['client_cert']
37-
sslclientkey: ['client_key']
38-
sslverify: ['validate_certs']
39-
ansible_builtin_copy:
40-
uniques: ['dest']
41-
aliases:
42-
attributes: ['attr']
43-
ansible_builtin_template:
44-
uniques: ['dest']
45-
aliases:
46-
attributes: ['attr']

roles/ansible/handlers/main.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
---
2+
# REVIEW: Execute the handler without when control, let the task decide to notify or not
3+
- name: Settle local facts on host if changed
4+
become: true
5+
ansible.builtin.template:
6+
src: "{{ ansible__local_fact_template }}"
7+
dest: "{{ ansible__local_fact_file }}"
8+
backup: "{{ ansible__local_fact_backup | default(omit) | bool }}"
9+
vars:
10+
on_host: "{{ ansible_local[ansible__local_fact_name] | default({}) | b64encode }}"
11+
on_runtime: "{{ ansible__local_facts | default({}) | b64encode }}"
12+
ansible_callback_diy_runner_on_skipped_msg: |
13+
skipping: [{{ inventory_hostname }}]
14+
msg: {{ (not ansible_check_mode and on_host != on_runtime) | ternary("Host local facts not changed.", "DRY-RUN") }}
15+
ansible_callback_diy_runner_on_skipped_msg_color: green
16+
when: on_host != on_runtime
17+
register: ansible__local_facts_settle
18+
listen: "ansible__local_facts_settle"
19+
notify: "ansible__local_facts_reread"
20+
221
- name: Re-read local facts on the host
322
become: true
423
ansible.builtin.setup:
524
filter: ansible_local
625
register: ansible__local_facts_reread
726
listen: "ansible__local_facts_reread"
27+
notify: ansible__local_facts_assign
28+
29+
- name: Assign host local facts to ansible facts
30+
ansible.builtin.set_fact:
31+
ansible__local_facts: "{{ ansible_local[ansible__local_fact_name] }}"
32+
register: ansible__local_facts_assign
33+
listen: "ansible__local_facts_assign"
34+
when:
35+
- ansible__local_fact_name is defined
36+
- ansible_local[ansible__local_fact_name] is defined

roles/ansible/tasks/assign_local_facts.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

roles/ansible/tasks/local_facts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
ansible.builtin.import_tasks:
1212
file: assign_local_facts.yml
1313
vars:
14-
ansible__updated_local_facts: "{{ ansible_local[__ansible__local_fact_name] | default({}) }}"
14+
ansible__updated_local_facts: "{{ ansible_local[ansible__local_fact_name] | default({}) }}"

roles/ansible/tasks/set_facts.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

roles/ansible/tasks/update_local_facts.yml

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,12 @@
44
updates: "{{ local_fact_updates }}"
55
vars:
66
update_paths: "{{ ansible__local_fact_updates | map(attribute='path') |
7-
map('regex_replace', '^ansible_local\\.' + __ansible__local_fact_name + '\\.|^ansible_local\\.', '') |
7+
map('regex_replace', '^ansible_local\\.' + ansible__local_fact_name + '\\.|^ansible_local\\.', '') |
88
map('regex_replace', '^(?!__ansible__local_facts\\.)(.*)$', '__ansible__local_facts.\\1') }}"
99
local_fact_updates: "{{ {'path': update_paths, 'value': (ansible__local_fact_updates | map(attribute='value'))} |
1010
aybarsm.helper.to_list_of_dicts }}"
1111
register: ansible__update_local_facts
12-
13-
- name: Import assign host local facts tasks for re-assignment
14-
ansible.builtin.import_tasks:
15-
file: assign_local_facts.yml
16-
vars:
17-
ansible__updated_local_facts: "{{ ansible__update_local_facts.__ansible__local_facts }}"
18-
19-
- name: Settle local facts on host if changed
20-
become: true
21-
ansible.builtin.template:
22-
src: "{{ ansible__local_fact_template }}"
23-
dest: "{{ ansible__local_fact_file }}"
24-
backup: "{{ ansible__local_fact_backup | default(omit) | bool }}"
25-
vars:
26-
on_host: "{{ ansible_local[__ansible__local_fact_name] | default({}) | b64encode }}"
27-
on_runtime: "{{ __ansible__local_facts | default({}) | b64encode }}"
28-
ansible_callback_diy_runner_on_skipped_msg: |
29-
skipping: [{{ inventory_hostname }}]
30-
msg: {{ (not ansible_check_mode and on_host != on_runtime) | ternary("Host local facts not changed.", "DRY-RUN") }}
31-
ansible_callback_diy_runner_on_skipped_msg_color: green
32-
when: on_host != on_runtime
3312
notify: "ansible__local_facts_reread"
34-
register: ansible__local_facts_settle
35-
36-
# - name: Re-read local facts from the host
37-
# become: true
38-
# ansible.builtin.setup:
39-
# filter: ansible_local
40-
# register: ansible__host_facts_reread
41-
# when: ansible__host_facts_settle.changed
4213

4314
- name: Re-read local facts on the host if they have been modified
4415
ansible.builtin.meta: 'flush_handlers'

roles/ansible/vars/main.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
__ansible__config:
2+
modules:
3+
ansible_builtin_apt:
4+
uniques: ['name']
5+
aliases:
6+
allow_downgrade: ['allow-downgrade', 'allow_downgrades', 'allow-downgrades']
7+
allow_unauthenticated: ['allow-unauthenticated']
8+
default_release: ['default-release']
9+
install_recommends: ['install-recommends']
10+
name: ['package', 'pkg']
11+
update_cache: ['update-cache']
12+
ansible_builtin_apt_repository:
13+
uniques: ['repo']
14+
aliases:
15+
update_cache: ['update-cache']
16+
ansible_builtin_apt_key:
17+
uniques: ['keyserver', 'url', 'id', 'file']
18+
ansible_builtin_dnf:
19+
uniques: ['name']
20+
aliases:
21+
name: ['package', 'pkg']
22+
update_cache: ['expire-cache']
23+
ansible_builtin_yum_repository:
24+
uniques: ['name']
25+
aliases:
26+
attributes: ['attr']
27+
sslcacert: ['ca_cert']
28+
sslclientcert: ['client_cert']
29+
sslclientkey: ['client_key']
30+
sslverify: ['validate_certs']
31+
ansible_builtin_copy:
32+
uniques: ['dest']
33+
aliases:
34+
attributes: ['attr']
35+
ansible_builtin_template:
36+
uniques: ['dest']
37+
aliases:
38+
attributes: ['attr']

0 commit comments

Comments
 (0)