Skip to content

Commit

Permalink
Restructure to comply with Ansible naming conventions - fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 3, 2024
1 parent 1f24d89 commit afc8d81
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 46 deletions.
8 changes: 4 additions & 4 deletions roles/ansible/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
role_ansible_enabled: true
linux_role_ansible_manage_local_facts: true
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__host_facts_dir: "{{ (not lookup('config', 'DEFAULT_FACT_PATH')) | ternary('/etc/ansible/facts.d', lookup('config', 'DEFAULT_FACT_PATH')) }}"
ansible__local_fact_file: "{{ ansible__host_facts_dir }}/aybarsm_linux.fact"
20 changes: 10 additions & 10 deletions roles/ansible/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
- name: Re-assign host local facts to ansible facts
become: true
ansible.builtin.set_fact:
__ansible__local_facts: "{{ ansible__update_local_facts.__ansible__local_facts }}"
register: ansible__local_facts_reassign
listen: "ansible__local_facts_reassign"
notify: ansible__local_facts_settle
__ansible__host_facts: "{{ ansible__update_host_facts.__ansible__host_facts }}"
register: ansible__host_facts_reassign
listen: "ansible__host_facts_reassign"
notify: ansible__host_facts_settle

- 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 }}"
register: ansible__local_facts_settle
listen: "ansible__local_facts_settle"
notify: ansible__local_facts_reread
when: (ansible_local.aybarsm_linux | b64encode) != (__ansible__local_facts | b64encode)
register: ansible__host_facts_settle
listen: "ansible__host_facts_settle"
notify: ansible__host_facts_reread
when: (ansible_local.aybarsm_linux | b64encode) != (__ansible__host_facts | b64encode)

- name: Re-read local facts
become: true
ansible.builtin.setup:
filter: ansible_local
register: ansible__local_facts_reread
listen: "ansible__local_facts_reread"
register: ansible__host_facts_reread
listen: "ansible__host_facts_reread"
6 changes: 4 additions & 2 deletions roles/ansible/tasks/local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
ansible.builtin.file:
state: directory
recurse: true
path: "{{ ansible__local_facts_dir }}"
path: "{{ ansible__host_facts_dir }}"

- name: Assign local facts as ansible facts
ansible.builtin.set_fact:
__ansible__local_facts: "{{ ansible_local.aybarsm_linux | default({}) }}"
__ansible__host_facts: "{{ ansible_local[fact_basename] | default({}) }}"
vars:
fact_basename: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}"
4 changes: 2 additions & 2 deletions roles/ansible/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
ansible.builtin.import_tasks:
file: local_facts.yml
when:
- role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
- ansible__role_enabled | bool
- ansible__manage_local_facts | bool
20 changes: 10 additions & 10 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
vars:
fact_basename: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}"
host_fact_updates: "{{ dict((
ansible_host_fact_updates | map(attribute='path') |
ansible__host_fact_updates | map(attribute='path') |
map('regex_replace', '^ansible_local\\.' + fact_basename + '\\.|^ansible_local\\.', '') |
map('regex_replace', '^(?!__ansible__local_facts\\.)(.*)$', '__ansible__local_facts.\\1')) |
zip(ansible_host_fact_updates | map(attribute='value'))) |
map('regex_replace', '^(?!__ansible__host_facts\\.)(.*)$', '__ansible__host_facts.\\1')) |
zip(ansible__host_fact_updates | map(attribute='value'))) |
dict2items(key_name='path', value_name='value') }}"
register: ansible__update_local_facts
register: ansible__update_host_facts

- name: Re-assign host local facts to ansible facts
ansible.builtin.set_fact:
__ansible__local_facts: "{{ ansible__update_local_facts.__ansible__local_facts }}"
register: ansible__local_facts_reassign
__ansible__host_facts: "{{ ansible__update_host_facts.__ansible__host_facts }}"
register: ansible__host_facts_reassign

- 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 }}"
register: ansible__local_facts_settle
register: ansible__host_facts_settle
vars:
fact_basename: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}"
on_host: "{{ ansible_local[fact_basename] | default({}) | b64encode }}"
on_runtime: "{{ __ansible__local_facts | default({}) | b64encode }}"
on_runtime: "{{ __ansible__host_facts | default({}) | b64encode }}"
when: on_host != on_runtime

- name: Re-read local facts
become: true
ansible.builtin.setup:
filter: ansible_local
register: ansible__local_facts_reread
when: ansible__local_facts_settle.changed
register: ansible__host_facts_reread
when: ansible__host_facts_settle.changed

# - name: Settle local facts if they have been modified
# ansible.builtin.meta: 'flush_handlers'
4 changes: 2 additions & 2 deletions roles/ansible/templates/aybarsm_linux.json.fact.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if __ansible__local_facts is defined %}
{{ __ansible__local_facts | to_nice_json }}
{% if __ansible__host_facts is defined %}
{{ __ansible__host_facts | to_nice_json }}
{% else %}
{
}
Expand Down
2 changes: 1 addition & 1 deletion roles/grub/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
grub_role_enabled: false
grub__role_enabled: false

grub__dir: /etc/default/grub.d
grub__file: 50-ansible.cfg
Expand Down
2 changes: 1 addition & 1 deletion roles/grub/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
- name: Import grub tasks
ansible.builtin.import_tasks:
file: grub.yml
when: grub_role_enabled | bool
when: grub__role_enabled | bool
2 changes: 1 addition & 1 deletion roles/network_interfaces/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
network_interfaces_role_enabled: false
network_interfaces__role_enabled: false

# Leave empty not to apply changes
network_interfaces__change_strategy: ''
Expand Down
2 changes: 1 addition & 1 deletion roles/network_interfaces/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
ansible.builtin.import_tasks:
file: network_interfaces.yml
when:
- systemd_network_role_enabled | bool
- systemd_network__role_enabled | bool
2 changes: 1 addition & 1 deletion roles/package_manager/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
package_manager_role_enabled: false
package_manager__role_enabled: false

# Change the package manager strategy to use for the system i.e. ansible.builtin.apt vs ansible.builtin.package
# Available options: specific, common
Expand Down
12 changes: 6 additions & 6 deletions roles/package_manager/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
ansible.builtin.import_tasks:
file: deb_initial_upgrade.yml
when:
- package_manager_role_enabled | default(false) | bool
- package_manager__role_enabled | default(false) | bool
- package_manager__initial_upgrade | default(false) | bool
- linux_role_ansible_manage_local_facts | default(false) | bool
- __ansible__local_facts is defined
- ansible__manage_local_facts | default(false) | bool
- __ansible__host_facts is defined
- ansible_os_family | lower == 'debian'

- name: Import DEB repository and repository key tasks (APT)
ansible.builtin.import_tasks:
file: deb.yml
when:
- package_manager_role_enabled | default(false) | bool
- package_manager__role_enabled | default(false) | bool
- ansible_os_family | lower == 'debian'

- name: Import DEB package tasks (APT)
ansible.builtin.import_tasks:
file: deb_packages.yml
when:
- package_manager_role_enabled | default(false) | bool
- package_manager__role_enabled | default(false) | bool
- package_manager__package_strategy | lower == 'specific'
- ansible_os_family | lower == 'debian'

- name: Import common package manager tasks
ansible.builtin.import_tasks:
file: common_packages.yml
when:
- package_manager_role_enabled | default(false) | bool
- package_manager__role_enabled | default(false) | bool
- package_manager__package_strategy | lower == 'common'
2 changes: 1 addition & 1 deletion roles/posix/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
posix_sysctl_role_enabled: false
posix_sysctl__role_enabled: false

posix__sysctl_default: []
posix__sysctl_group: []
Expand Down
2 changes: 1 addition & 1 deletion roles/posix/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
- name: Import posix sysctl tasks
ansible.builtin.import_tasks:
file: sysctl.yml
when: posix_sysctl_role_enabled | bool
when: posix_sysctl__role_enabled | bool
2 changes: 1 addition & 1 deletion roles/proxmox/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
proxmox_role_enabled: false
proxmox__role_enabled: false

proxmox__clean_os_upgrade: ''

Expand Down
2 changes: 1 addition & 1 deletion roles/systemd/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# Ensures system manager is systemd - ansible_service_mgr == "systemd"
systemd__ensure_service_manager: true
systemd_network_role_enabled: false
systemd_network__role_enabled: false

systemd__network_apply_changes: false

Expand Down
2 changes: 1 addition & 1 deletion roles/systemd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
ansible.builtin.import_tasks:
file: network.yml
when:
- systemd_network_role_enabled | bool
- systemd_network__role_enabled | bool
- not systemd__ensure_service_manager | bool or ansible_service_mgr == "systemd"

0 comments on commit afc8d81

Please sign in to comment.