Skip to content

Commit

Permalink
support for defining the organization (#887)
Browse files Browse the repository at this point in the history
* feat: support for defined organization

* misc: set default org as organizationless, add conditions into templates, add overlooked required changes

* fix: brackets and missing changes

* fix: brackets part2

* rev: wrong change in file

---------

Co-authored-by: Przemyslaw Kalitowski <przemyslaw@kalitowski.com>
  • Loading branch information
przemkalit and Przemyslaw Kalitowski authored Aug 5, 2024
1 parent 329097a commit 0c490f8
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 54 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/filetree_create_default_org.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- filetree_create able to use defined organization for organizationless objects
1 change: 1 addition & 0 deletions roles/filetree_create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following variables are required for that role to work properly:
| `flatten_output` | N/A | no | bool | Whether to flatten the output in single files per each object type instead of the normal exportation structure |
| `show_encrypted` | N/A | no | bool | Whether to remove the string '\$encrypted\$' in credentials output (not the actual credential value) |
| `omit_id` | N/A | no | bool | Whether to create output files without objects id.|
| `organization`| N/A | no | str | Default organization for all objects that have not been set in the source controller.|

## Dependencies

Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ controller_configuration_filetree_create_secure_logging: "{{ controller_configur

input_tag:
- all

organization: 'ORGANIZATIONLESS'
...
13 changes: 8 additions & 5 deletions roles/filetree_create/tasks/applications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
marker: ""
block: "{{ lookup('template', 'templates/current_applications.j2') }}"
vars:
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true) }}"
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default(organization, true) }}"
application_id: "{{ current_applications_asset_value.id }}"
application_name: "{{ current_applications_asset_value.name | regex_replace('/', '_') }}"
last_application: "{{ current_application_index == ((applications_lookvar | length) - 1) }}"
Expand All @@ -53,9 +53,12 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/applications"
loop: "{{ (applications_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((applications_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop: >-
{{ (applications_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([default(organization)] if ((applications_lookvar | map(attribute='summary_fields')
| selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand All @@ -66,7 +69,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true) }}"
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default(organization, true) }}"
application_id: "{{ current_applications_asset_value.id }}"
application_name: "{{ current_applications_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ application_organization | regex_replace('/', '_') }}/applications/{{ (application_id ~ '_') if omit_id is not defined else '' }}{{ application_name | regex_replace('/', '_') }}.yaml"
Expand Down
6 changes: 3 additions & 3 deletions roles/filetree_create/tasks/constructed_inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
marker: ""
block: "{{ lookup('template', 'templates/current_inventories.j2') }}"
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventories_asset_value.name | regex_replace('/', '_') }}"
first_inventory: "{{ not (__constructed_inventories_file.stat.exists | bool) }}"
last_inventory: "{{ current_inventory_index == ((constructed_inventory_lookvar | length) - 1) }}"
Expand All @@ -64,7 +64,7 @@
state: directory
mode: '0755'
vars:
inventory_organization: "{{ needed_path.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ needed_path.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ needed_path.name | regex_replace('/', '_') }}"
__path: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}"
loop: "{{ constructed_inventory_lookvar }}"
Expand All @@ -78,7 +78,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventories_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}/{{ (current_inventories_asset_value.id ~ '_') if omit_id is not defined else '' }}{{ inventory_name | regex_replace('/', '_') }}.yaml"
loop: "{{ constructed_inventory_lookvar }}"
Expand Down
13 changes: 8 additions & 5 deletions roles/filetree_create/tasks/credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
marker: ""
block: "{{ lookup('template', 'templates/current_credentials.j2') }}"
vars:
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default(organization) }}"
credentials_id: "{{ current_credentials_asset_value.id }}"
credentials_name: "{{ current_credentials_asset_value.name | regex_replace('/', '_') }}"
last_credential: "{{ current_credential_index == ((credentials_lookvar | length) - 1) }}"
Expand All @@ -53,9 +53,12 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path }}/credentials"
loop: "{{ (credentials_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((credentials_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop: >-
{{ (credentials_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ ([(organization if organization is defined else 'ORGANIZATIONLESS')] if ((credentials_lookvar |
map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand All @@ -66,7 +69,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default(organization) }}"
credentials_id: "{{ current_credentials_asset_value.id }}"
credentials_name: "{{ current_credentials_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ credentials_organization | regex_replace('/', '_') }}/credentials/{{ (credentials_id ~ '_') if omit_id is not defined else '' }}{{ credentials_name | regex_replace('/', '_') }}.yaml"
Expand Down
12 changes: 6 additions & 6 deletions roles/filetree_create/tasks/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
marker: ''
block: "{{ lookup('template', 'templates/current_inventories.j2') }}"
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventories_asset_value.name | regex_replace('/', '_') }}"
first_inventory: "{{ not (__inventories_file.stat.exists | bool) }}"
last_inventory: "{{ current_inventory_index == ((inventory_lookvar | length) - 1) }}"
Expand All @@ -65,7 +65,7 @@
state: directory
mode: '0755'
vars:
inventory_organization: "{{ needed_path.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ needed_path.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ needed_path.name | regex_replace('/', '_') }}"
__path: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}"
loop: "{{ inventory_lookvar }}"
Expand All @@ -79,7 +79,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventories_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}/{{ (current_inventories_asset_value.id ~ '_') if omit_id is not defined else '' }}{{ inventory_name | regex_replace('/', '_') }}.yaml"
loop: "{{ inventory_lookvar }}"
Expand All @@ -91,7 +91,7 @@
ansible.builtin.include_tasks: "inventory_sources.yml"
when: current_inventory_sources.total_inventory_sources > 0
vars:
inventory_organization: "{{ current_inventory_sources.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventory_sources.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventory_sources.name | regex_replace('/', '_') }}"
inventory_sources_output_path: "{{ (output_path + '/' + inventory_organization | regex_replace('/', '_') + '/inventories/' + inventory_name | regex_replace('/', '_'))
if (flatten_output is not defined or (flatten_output | bool) == false)
Expand All @@ -113,7 +113,7 @@
- name: "Set the inventory's hosts"
ansible.builtin.include_tasks: "hosts.yml"
vars:
inventory_organization: "{{ current_inventory_hosts.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventory_hosts.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventory_hosts.name | regex_replace('/', '_') }}"
hosts_output_path: "{{ (output_path + '/' + inventory_organization | regex_replace('/', '_') + '/inventories/' + inventory_name | regex_replace('/', '_'))
if (flatten_output is not defined or (flatten_output | bool) == false)
Expand All @@ -136,7 +136,7 @@
ansible.builtin.include_tasks: "groups.yml"
when: current_inventory_groups.total_groups > 0
vars:
inventory_organization: "{{ current_inventory_groups.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_organization: "{{ current_inventory_groups.summary_fields.organization.name | default(organization) }}"
inventory_name: "{{ current_inventory_groups.name | regex_replace('/', '_') }}"
groups_output_path: "{{ (output_path + '/' + inventory_organization | regex_replace('/', '_') + '/inventories/' + inventory_name | regex_replace('/', '_'))
if (flatten_output is not defined or (flatten_output | bool) == false)
Expand Down
13 changes: 8 additions & 5 deletions roles/filetree_create/tasks/job_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
marker: ""
block: "{{ lookup('template', 'templates/current_job_templates.j2') }}"
vars:
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default(organization) }}"
job_template_id: "{{ current_job_templates_asset_value.id }}"
job_template_name: "{{ current_job_templates_asset_value.name | regex_replace('/', '_') }}"
query_labels: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.labels,
Expand Down Expand Up @@ -68,9 +68,12 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/job_templates"
loop: "{{ (job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop: >-
{{ (job_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (job_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand All @@ -81,7 +84,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default(organization) }}"
job_template_id: "{{ current_job_templates_asset_value.id }}"
job_template_name: "{{ current_job_templates_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ job_template_organization | regex_replace('/', '_') }}/job_templates/{{ (job_template_id ~ '_') if omit_id is not defined else '' }}{{ job_template_name | regex_replace('/', '_') }}.yaml"
Expand Down
2 changes: 1 addition & 1 deletion roles/filetree_create/tasks/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
label_organization: "{{ current_labels_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true) }}"
label_organization: "{{ current_labels_asset_value.summary_fields.organization.name | default(organization, true) }}"
label_id: "{{ current_labels_asset_value.id }}"
label_name: "{{ current_labels_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ label_organization | regex_replace('/', '_') }}/labels/{{ (label_id ~ '_') if omit_id is not defined else '' }}{{ label_name | regex_replace('/', '_') }}.yaml"
Expand Down
11 changes: 7 additions & 4 deletions roles/filetree_create/tasks/notification_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/notification_templates"
loop: "{{ (notification_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((notification_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop: >-
{{ (notification_templates_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (notification_templates_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand All @@ -63,7 +66,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
__dest: "{{ output_path }}/{{ (current_notification_templates_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}/notification_templates/{{ current_notification_templates_asset_value.name | regex_replace('/', '_') }}.yaml"
__dest: "{{ output_path }}/{{ (current_notification_templates_asset_value.summary_fields.organization.name |default(organization, true)) | regex_replace('/', '_') }}/notification_templates/{{ current_notification_templates_asset_value.name | regex_replace('/', '_') }}.yaml"
loop: "{{ notification_templates_lookvar }}"
loop_control:
loop_var: current_notification_templates_asset_value
Expand Down
13 changes: 8 additions & 5 deletions roles/filetree_create/tasks/projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
marker: ""
block: "{{ lookup('template', 'templates/current_projects.j2') }}"
vars:
project_organization: "{{ current_projects_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true) }}"
project_organization: "{{ current_projects_asset_value.summary_fields.organization.name | default(organization, true) }}"
project_id: "{{ current_projects_asset_value.id }}"
project_name: "{{ current_projects_asset_value.name | regex_replace('/', '_') }}"
query_notification_error: "{{ query(controller_api_plugin, current_projects_asset_value.related.notification_templates_error,
Expand Down Expand Up @@ -64,9 +64,12 @@
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/projects"
loop: "{{ (projects_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((projects_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop: >-
{{ (projects_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') |
map(attribute='organization') | map(attribute='name') | list | flatten | unique) +
[organization] if (projects_lookvar | map(attribute='summary_fields') |
selectattr('organization', 'undefined') | list | flatten | length > 0) else []
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
Expand All @@ -77,7 +80,7 @@
dest: "{{ __dest }}"
mode: '0644'
vars:
project_organization: "{{ current_projects_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true) }}"
project_organization: "{{ current_projects_asset_value.summary_fields.organization.name | default(organization, true) }}"
project_id: "{{ current_projects_asset_value.id }}"
project_name: "{{ current_projects_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ project_organization | regex_replace('/', '_') }}/projects/{{ (project_id ~ '_') if omit_id is not defined else '' }}{{ project_name | regex_replace('/', '_') }}.yaml"
Expand Down
Loading

0 comments on commit 0c490f8

Please sign in to comment.