Skip to content

Commit

Permalink
all the objects has been updated to let flatten output
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarmu committed Nov 7, 2023
1 parent 0a83891 commit 2f79c15
Show file tree
Hide file tree
Showing 37 changed files with 1,038 additions and 386 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/filtree_create_flatten_output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- The role 'filetree_create' will now allow to export all the objects of one kind into a single file, so it can be loaded by both ansible `group_vars` syntax and `filetree_read` tool.
...
1 change: 1 addition & 0 deletions roles/filetree_create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following variables are required for that role to work properly:
| `organization_id` | N/A | no | int | Alternative to `organization_filter`, but specifiying the current organization's ID to filter by. Exports only the objects belonging to the specified organization (applies to all the objects that can be assigned to an organization). |
| `output_path` | `/tmp/filetree_output` | yes | str | The path to the output directory where all the generated `yaml` files with the corresponding Objects as code will be written to. |
| `input_tag` | `['all']` | no | List of Strings | The tags which are applied to the 'sub-roles'. If 'all' is in the list (the default value) then all roles will be called. |
| `flatten_output` | N/A | no | bool | Whether to flatten the output in single files per each object type instead of the normal exportation structure |

## Dependencies

Expand Down
86 changes: 60 additions & 26 deletions roles/filetree_create/tasks/applications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,66 @@
order_by: 'organization,id'
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the <ORGANIZATION_NAME>/applications output directory for applications in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
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_control:
loop_var: needed_path
label: "{{ __path }}"
__dest: "{{ output_path }}/applications.yaml"
block:
- name: "Add current applications to the applications flat file"

Check failure on line 21 in roles/filetree_create/tasks/applications.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

risky-file-permissions

File permissions unset or incorrect.
ansible.builtin.blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_applications.j2') }}"
vars:
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', 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) }}"
loop: "{{ applications_lookvar }}"
loop_control:
index_var: current_application_index
loop_var: current_applications_asset_value
label: "{{ __dest }}"

- name: "Add current applications to the <ORGANIZATION_NAME>/applications output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_applications.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', 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 }}_{{ application_name | regex_replace('/', '_') }}.yaml"
loop: "{{ applications_lookvar }}"
loop_control:
loop_var: current_applications_asset_value
label: "{{ __dest }}"
- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
path: "{{ __dest }}"
line: ''
state: absent

- name: "Block for to generate the filetre_create normal output"

Check failure on line 45 in roles/filetree_create/tasks/applications.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

literal-compare

Don't compare to literal True/False.
when: flatten_output is not defined or (flatten_output | bool) == false
block:
- name: "Create the <ORGANIZATION_NAME>/applications output directory for applications in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
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_control:
loop_var: needed_path
label: "{{ __path }}"

- name: "Add current applications to the <ORGANIZATION_NAME>/applications output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_applications.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
application_organization: "{{ current_applications_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', 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 }}_{{ application_name | regex_replace('/', '_') }}.yaml"
loop: "{{ applications_lookvar }}"
loop_control:
loop_var: current_applications_asset_value
label: "{{ __dest }}"
...
98 changes: 72 additions & 26 deletions roles/filetree_create/tasks/constructed_inventory.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: "Get the inventories from the API"
- name: "Get the constructed inventories from the API"
ansible.builtin.set_fact:
constructed_inventory_lookvar: "{{ query(controller_api_plugin, 'api/v2/constructed_inventories/',
query_params=(query_params | combine({'organization': organization_id})) if organization_id is defined else query_params,
Expand All @@ -11,31 +11,77 @@
order_by: 'organization,id'
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the <ORGANIZATION_NAME>/inventories output directory for inventories in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
vars:
inventory_organization: "{{ needed_path.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_name: "{{ needed_path.name | regex_replace('/', '_') }}"
__path: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}"
loop: "{{ constructed_inventory_lookvar }}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
__dest: "{{ output_path }}/inventories.yaml"
block:
- name: "Stat if the output file exists"
ansible.builtin.stat:
path: "{{ __dest }}"
register: __constructed_inventories_file

- name: "Add current constructed_inventories to the <ORGANIZATION_NAME>/inventories output yaml file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_inventories.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ inventory_name | regex_replace('/', '_') }}.yaml"
loop: "{{ constructed_inventory_lookvar }}"
loop_control:
loop_var: current_inventories_asset_value
label: "{{ __dest }}"
- name: "Remove the yaml finalizer if it's already present"

Check failure on line 26 in roles/filetree_create/tasks/constructed_inventory.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

fqcn[action-core]

Use FQCN for builtin module actions (lineinfile).
lineinfile:
path: "{{ __dest }}"
line: "..."
state: absent
when: __constructed_inventories_file.stat.exists | bool

- name: "Add current constructed inventory to the inventories flat file"

Check failure on line 33 in roles/filetree_create/tasks/constructed_inventory.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

risky-file-permissions

File permissions unset or incorrect.
ansible.builtin.blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_inventories.j2') }}"
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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) }}"
loop: "{{ constructed_inventory_lookvar }}"
loop_control:
index_var: current_inventory_index
loop_var: current_inventories_asset_value
label: "{{ __dest }}"

- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
path: "{{ __dest }}"
line: ''
state: absent

- name: "Block for to generate the filetre_create normal output"

Check failure on line 57 in roles/filetree_create/tasks/constructed_inventory.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

literal-compare

Don't compare to literal True/False.
when: flatten_output is not defined or (flatten_output | bool) == false
block:
- name: "Create the <ORGANIZATION_NAME>/inventories output directory for inventories in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
vars:
inventory_organization: "{{ needed_path.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
inventory_name: "{{ needed_path.name | regex_replace('/', '_') }}"
__path: "{{ output_path }}/{{ inventory_organization | regex_replace('/', '_') }}/inventories/{{ inventory_name | regex_replace('/', '_') }}"
loop: "{{ constructed_inventory_lookvar }}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"

- name: "Add current constructed_inventories to the <ORGANIZATION_NAME>/inventories output yaml file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_inventories.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
inventory_organization: "{{ current_inventories_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ inventory_name | regex_replace('/', '_') }}.yaml"
loop: "{{ constructed_inventory_lookvar }}"
loop_control:
loop_var: current_inventories_asset_value
label: "{{ __dest }}"
...
86 changes: 60 additions & 26 deletions roles/filetree_create/tasks/credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,66 @@
order_by: 'organization,id'
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the <ORGANIZATION_NAME>/credentials output directory for credentials in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
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_control:
loop_var: needed_path
label: "{{ __path }}"
__dest: "{{ output_path }}/credentials.yaml"
block:
- name: "Add current credentials to the credentials flat file"

Check failure on line 21 in roles/filetree_create/tasks/credentials.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

risky-file-permissions

File permissions unset or incorrect.
ansible.builtin.blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_credentials.j2') }}"
vars:
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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) }}"
loop: "{{ credentials_lookvar }}"
loop_control:
index_var: current_credential_index
loop_var: current_credentials_asset_value
label: "{{ __dest }}"

- name: "Add current credentials to the <ORGANIZATION_NAME>/credentials output yaml file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_credentials.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ credentials_name | regex_replace('/', '_') }}.yaml"
loop: "{{ credentials_lookvar }}"
loop_control:
loop_var: current_credentials_asset_value
label: "{{ __dest }}"
- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
path: "{{ __dest }}"
line: ''
state: absent

- name: "Block for to generate the filetre_create normal output"

Check failure on line 45 in roles/filetree_create/tasks/credentials.yml

View workflow job for this annotation

GitHub Actions / pre-commit_and_sanity / pre-commit / pre-commit

literal-compare

Don't compare to literal True/False.
when: flatten_output is not defined or (flatten_output | bool) == false
block:
- name: "Create the <ORGANIZATION_NAME>/credentials output directory for credentials in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
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_control:
loop_var: needed_path
label: "{{ __path }}"

- name: "Add current credentials to the <ORGANIZATION_NAME>/credentials output yaml file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_credentials.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
credentials_organization: "{{ current_credentials_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ credentials_name | regex_replace('/', '_') }}.yaml"
loop: "{{ credentials_lookvar }}"
loop_control:
loop_var: current_credentials_asset_value
label: "{{ __dest }}"
...
46 changes: 35 additions & 11 deletions roles/filetree_create/tasks/groups.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
---
- name: "Create the output directory for groups: {{ groups_output_path }}"
ansible.builtin.file:
path: "{{ groups_output_path }}"
state: directory
mode: '0755'
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
vars:
__dest: "{{ groups_output_path }}"
block:
- name: "Add current groups to the groups flat file"
ansible.builtin.blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_groups.j2') }}"

- name: "Add current groups to the current_groups.yaml output file in {{ groups_output_path }}"
ansible.builtin.template:
src: "templates/current_groups.j2"
dest: "{{ groups_output_path }}/current_groups.yaml"
mode: '0644'
when: current_groups_asset_value | length > 0
- name: "Remove all the blank lines introduced by the last task"
ansible.builtin.lineinfile:
path: "{{ __dest }}"
line: ''
state: absent

- name: "Block for to generate the filetre_create normal output"
when: flatten_output is not defined or (flatten_output | bool) == false
block:
- name: "Create the output directory for groups: {{ groups_output_path }}"
ansible.builtin.file:
path: "{{ groups_output_path }}"
state: directory
mode: '0755'

- name: "Add current groups to the current_groups.yaml output file in {{ groups_output_path }}"
ansible.builtin.template:
src: "templates/current_groups.j2"
dest: "{{ groups_output_path }}/current_groups.yaml"
mode: '0644'
when: current_groups_asset_value | length > 0
...
Loading

0 comments on commit 2f79c15

Please sign in to comment.