Skip to content

Commit

Permalink
Restructure to comply with Ansible naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
aybarsm committed Jul 3, 2024
1 parent 9037600 commit 1f24d89
Show file tree
Hide file tree
Showing 37 changed files with 302 additions and 293 deletions.
10 changes: 5 additions & 5 deletions roles/ansible/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
linux_role_ansible_enabled: true
role_ansible_enabled: true
linux_role_ansible_manage_local_facts: true

linux_ansible_local_fact_template: aybarsm_linux.json.fact.j2
linux_ansible_local_fact_backup: true
linux_ansible_local_facts_dir: "{{ (not lookup('config', 'DEFAULT_FACT_PATH')) | ternary('/etc/ansible/facts.d', lookup('config', 'DEFAULT_FACT_PATH')) }}"
linux_ansible_local_fact_file: "{{ linux_ansible_local_facts_dir }}/aybarsm_linux.fact"
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"
26 changes: 13 additions & 13 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:
__linux_ansible_local_facts: "{{ linux_ansible_update_local_facts.__linux_ansible_local_facts }}"
register: linux_ansible_local_facts_reassign
listen: "linux_ansible_local_facts_reassign"
notify: linux_ansible_local_facts_settle
__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

- name: Settle local facts on host if changed
become: true
ansible.builtin.template:
src: "{{ linux_ansible_local_fact_template }}"
dest: "{{ linux_ansible_local_fact_file }}"
backup: "{{ linux_ansible_local_fact_backup | default(omit) | bool }}"
register: linux_ansible_local_facts_settle
listen: "linux_ansible_local_facts_settle"
notify: linux_ansible_local_facts_reread
when: (ansible_local.aybarsm_linux | b64encode) != (__linux_ansible_local_facts | b64encode)
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)

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

- name: Assign local facts as ansible facts
ansible.builtin.set_fact:
__linux_ansible_local_facts: "{{ ansible_local.aybarsm_linux | default({}) }}"
__ansible__local_facts: "{{ ansible_local.aybarsm_linux | default({}) }}"
2 changes: 1 addition & 1 deletion 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:
- linux_role_ansible_enabled | bool
- role_ansible_enabled | bool
- linux_role_ansible_manage_local_facts | bool
26 changes: 13 additions & 13 deletions roles/ansible/tasks/update_local_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
ansible.utils.update_fact:
updates: "{{ host_fact_updates }}"
vars:
fact_basename: "{{ linux_ansible_local_fact_file | basename | regex_replace('\\.fact$', '') }}"
fact_basename: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}"
host_fact_updates: "{{ dict((
ansible_host_fact_updates | map(attribute='path') |
map('regex_replace', '^ansible_local\\.' + fact_basename + '\\.|^ansible_local\\.', '') |
map('regex_replace', '^(?!__linux_ansible_local_facts\\.)(.*)$', '__linux_ansible_local_facts.\\1')) |
map('regex_replace', '^(?!__ansible__local_facts\\.)(.*)$', '__ansible__local_facts.\\1')) |
zip(ansible_host_fact_updates | map(attribute='value'))) |
dict2items(key_name='path', value_name='value') }}"
register: linux_ansible_update_local_facts
register: ansible__update_local_facts

- name: Re-assign host local facts to ansible facts
ansible.builtin.set_fact:
__linux_ansible_local_facts: "{{ linux_ansible_update_local_facts.__linux_ansible_local_facts }}"
register: linux_ansible_local_facts_reassign
__ansible__local_facts: "{{ ansible__update_local_facts.__ansible__local_facts }}"
register: ansible__local_facts_reassign

- name: Settle local facts on host if changed
become: true
ansible.builtin.template:
src: "{{ linux_ansible_local_fact_template }}"
dest: "{{ linux_ansible_local_fact_file }}"
backup: "{{ linux_ansible_local_fact_backup | default(omit) | bool }}"
register: linux_ansible_local_facts_settle
src: "{{ ansible__local_fact_template }}"
dest: "{{ ansible__local_fact_file }}"
backup: "{{ ansible__local_fact_backup | default(omit) | bool }}"
register: ansible__local_facts_settle
vars:
fact_basename: "{{ linux_ansible_local_fact_file | basename | regex_replace('\\.fact$', '') }}"
fact_basename: "{{ ansible__local_fact_file | basename | regex_replace('\\.fact$', '') }}"
on_host: "{{ ansible_local[fact_basename] | default({}) | b64encode }}"
on_runtime: "{{ __linux_ansible_local_facts | default({}) | b64encode }}"
on_runtime: "{{ __ansible__local_facts | default({}) | b64encode }}"
when: on_host != on_runtime

- name: Re-read local facts
become: true
ansible.builtin.setup:
filter: ansible_local
register: linux_ansible_local_facts_reread
when: linux_ansible_local_facts_settle.changed
register: ansible__local_facts_reread
when: ansible__local_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 __linux_ansible_local_facts is defined %}
{{ __linux_ansible_local_facts | to_nice_json }}
{% if __ansible__local_facts is defined %}
{{ __ansible__local_facts | to_nice_json }}
{% else %}
{
}
Expand Down
34 changes: 17 additions & 17 deletions roles/grub/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
---
linux_role_grub_enabled: false
grub_role_enabled: false

linux_grub_dir: /etc/default/grub.d
linux_grub_file: 50-ansible.cfg
linux_grub_destination: "{{ linux_grub_dir + '/' + linux_grub_file }}"
grub__dir: /etc/default/grub.d
grub__file: 50-ansible.cfg
grub__destination: "{{ grub__dir + '/' + grub__file }}"

linux_grub_backup: true
linux_grub_template: grub.cfg.j2
grub__backup: true
grub__template: grub.cfg.j2

# Currently only command strategy is supported
# linux_grub_change_strategy: 'command'
# grub__change_strategy: 'command'
# Leave empty not to apply changes
linux_grub_change_strategy: ''
linux_grub_change:
grub__change_strategy: ''
grub__change:
- cmd: update-grub

linux_grub_entry_prefix: GRUB_
grub__entry_prefix: GRUB_

linux_grub_inherit_cmdline_default: true
linux_grub_inherit_cmdline: true
grub__inherit_cmdline_default: true
grub__inherit_cmdline: true

linux_grub_default: []
linux_grub_group: []
linux_grub_host: []
grub__default: []
grub__group: []
grub__host: []
# The combination strategy below is highly nested hierarchy compliant and recommended (Example provided below)
# linux_grub_host > linux_grub_group > linux_grub_default
linux_grub_all: "{{ [linux_grub_default, linux_grub_group, linux_grub_host] |
# grub__host > grub__group > grub__default
grub__all: "{{ [grub__default, grub__group, grub__host] |
community.general.lists_mergeby('name', recursive=true, list_merge='prepend') |
aybarsm.helper.unique_recursive(attributes='name', recurse='value') }}"
14 changes: 7 additions & 7 deletions roles/grub/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Apply command(s) for linux_grub changes
- name: Apply command(s) for grub_ changes
become: true
ansible.builtin.command:
chdir: "{{ item.chdir | default(omit) }}"
Expand All @@ -10,10 +10,10 @@
stdin_add_newline: "{{ item.stdin_add_newline | default(omit) }}"
strip_empty_ends: "{{ item.strip_empty_ends | default(omit) }}"
changed_when: true
loop: "{{ linux_grub_change | selectattr('cmd', 'defined') }}"
register: linux_grub_apply_changes_command
listen: "linux_grub_apply_changes_by_command"
loop: "{{ grub__change | selectattr('cmd', 'defined') }}"
register: grub__apply_changes_command
listen: "grub__apply_changes_by_command"
when:
- linux_grub_change_strategy | default('') == 'command'
- linux_grub_change | type_debug == 'list'
- linux_grub_change | selectattr('cmd', 'defined') | length > 0
- grub__change_strategy | default('') == 'command'
- grub__change | type_debug == 'list'
- grub__change | selectattr('cmd', 'defined') | length > 0
14 changes: 7 additions & 7 deletions roles/grub/tasks/grub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
- name: Deploy grub.cfg
become: true
ansible.builtin.template:
src: "{{ linux_grub_template }}"
dest: "{{ linux_grub_destination }}"
backup: "{{ linux_grub_backup | default(omit) | bool }}"
src: "{{ grub__template }}"
dest: "{{ grub__destination }}"
backup: "{{ grub__backup | default(omit) | bool }}"
mode: "0644"
when:
- linux_grub_all | type_debug == 'list'
- linux_grub_all | length > 0
register: linux_grub_deploy
notify: linux_grub_apply_changes_by_command
- grub__all | type_debug == 'list'
- grub__all | length > 0
register: grub__deploy
notify: grub__apply_changes_by_command
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: linux_role_grub_enabled | bool
when: grub_role_enabled | bool
8 changes: 4 additions & 4 deletions roles/grub/templates/grub.cfg.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# {{ ansible_managed }}

{% for param in linux_grub_all %}
{% for param in grub__all %}
{% if (param | type_debug) == 'dict' and 'name' in param %}
{% set paramName = (linux_grub_entry_prefix | upper) ~ (param.name | upper) %}
{% set paramName = (grub__entry_prefix | upper) ~ (param.name | upper) %}
{% if 'value' in param %}
{% if (param.value | type_debug) == 'list' %}
{% set inheritCmdlineDefault = param.name | lower == 'cmdline_linux_default' and linux_grub_inherit_cmdline_default | bool %}
{% set inheritCmdline = param.name | lower == 'cmdline_linux' and linux_grub_inherit_cmdline | bool %}
{% set inheritCmdlineDefault = param.name | lower == 'cmdline_linux_default' and grub__inherit_cmdline_default | bool %}
{% set inheritCmdline = param.name | lower == 'cmdline_linux' and grub__inherit_cmdline | bool %}
{% if inheritCmdlineDefault or inheritCmdline %}
{% set paramValPrefix = '$' ~ paramName ~ ' ' %}
{% else %}
Expand Down
60 changes: 30 additions & 30 deletions roles/network_interfaces/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
---
linux_role_network_interfaces_enabled: false
network_interfaces_role_enabled: false

# Leave empty not to apply changes
linux_network_interfaces_change_strategy: ''
network_interfaces__change_strategy: ''

# Example for apply changes via command strategy:
# linux_network_interfaces_change_strategy: command
# linux_network_interfaces_change:
# network_interfaces__change_strategy: command
# network_interfaces__change:
# - cmd: ifreload -a

# Example for apply changes via service strategy:
# linux_network_interfaces_change_strategy: service
# linux_network_interfaces_change:
# network_interfaces__change_strategy: service
# network_interfaces__change:
# - name: networking
# state: restarted

linux_network_interfaces_dir: /etc/network
linux_network_interfaces_file: interfaces
linux_network_interfaces_file_destination: "{{ linux_network_interfaces_dir + '/' + linux_network_interfaces_file }}"
network_interfaces__dir: /etc/network
network_interfaces__file: interfaces
network_interfaces__file_destination: "{{ network_interfaces__dir + '/' + network_interfaces__file }}"

# Seggrate network interfaces into separate files i.e. /etc/network/interfaces.d/eth0
# This has not been implemented yet
# TODO: Implement this feature
linux_network_interfaces_segregate: false
linux_network_interfaces_seggregate_dir: "{{ linux_network_interfaces_dir }}/interfaces.d"
network_interfaces__segregate: false
network_interfaces__seggregate_dir: "{{ network_interfaces__dir }}/interfaces.d"

# Keep the lo interface in the file
linux_network_interfaces_keep_lo: true
network_interfaces__keep_lo: true
# The location of the source line in the file (controversial topic)
linux_network_interfaces_source_position: bottom
linux_network_interfaces_source_line: "source {{ linux_network_interfaces_seggregate_dir }}/*"
network_interfaces__source_position: bottom
network_interfaces__source_line: "source {{ network_interfaces__seggregate_dir }}/*"

linux_network_interfaces_backup: true
linux_network_interfaces_template: network_interfaces.j2
network_interfaces__backup: true
network_interfaces__template: network_interfaces.j2
# This has not been implemented yet
# TODO: Implement this feature
linux_network_interfaces_iface_template: network_interfaces.iface.j2
network_interfaces__iface_template: network_interfaces.iface.j2

# iface XXX inet {manual|static|dhcp}
# Manual method only INCLUDES selected options whereas static and dhcp methods EXCLUDES selected options
linux_network_interfaces_manual_includes: []
linux_network_interfaces_static_excludes: []
linux_network_interfaces_dhcp_excludes: [
network_interfaces__manual_includes: []
network_interfaces__static_excludes: []
network_interfaces__dhcp_excludes: [
'address', 'netmask', 'gateway', 'broadcast', 'network', 'dns-nameservers', 'dns-search',
'dns-domain', 'dns-domain-search', 'dns-options', 'dns-sortlist', 'dns-opts']

linux_network_interfaces_default: []
linux_network_interfaces_group: []
linux_network_interfaces_host: []
network_interfaces__default: []
network_interfaces__group: []
network_interfaces__host: []

# The combination strategy below is highly nested hierarchy compliant and recommended (Example provided below)
# linux_network_interfaces_host > linux_network_interfaces_group > linux_network_interfaces_default
linux_network_interfaces_all: "{{
[linux_network_interfaces_default, linux_network_interfaces_group, linux_network_interfaces_host] |
# network_interfaces__host > network_interfaces__group > network_interfaces__default
network_interfaces__all: "{{
[network_interfaces__default, network_interfaces__group, network_interfaces__host] |
community.general.lists_mergeby('name', recursive=true, list_merge='prepend') |
aybarsm.helper.unique_recursive(attributes='name', recurse='inet') | aybarsm.helper.unique_recursive(attributes='name', recurse='inet6') }}"

# linux_network_interfaces_default:
# network_interfaces__default:
# - name: enp0s6
# mount: auto
# - name: vmbr0
Expand All @@ -66,7 +66,7 @@ linux_network_interfaces_all: "{{
# - name: bridge-stp
# value: "on"

# linux_network_interfaces_group:
# network_interfaces__group:
# - name: vmbr0
# mount: auto
# inet:
Expand All @@ -77,7 +77,7 @@ linux_network_interfaces_all: "{{
# - name: bridge-fd
# value: 0

# linux_network_interfaces_host:
# network_interfaces__host:
# - name: enp0s6
# mount: None
# - name: vmbr0
Expand All @@ -93,7 +93,7 @@ linux_network_interfaces_all: "{{
# value: fdff::1/64

# !!!!! RESULT: !!!!!
# linux_network_interfaces_all:
# network_interfaces__all:
# - name: enp0s6
# mount: None # Replaced from default by host
# - name: vmbr0
Expand Down
Loading

0 comments on commit 1f24d89

Please sign in to comment.