Skip to content

Commit

Permalink
playbook to convert from filetree_create to flat files. job_templates…
Browse files Browse the repository at this point in the history
…, teams and team_roles to direct flat output
  • Loading branch information
ivarmu committed Nov 6, 2023
1 parent a79f987 commit 0a83891
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 82 deletions.
85 changes: 85 additions & 0 deletions playbooks/flatten_filetree_create_output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
- name: "Flatten the filetree_create output into unique files per each object type"
hosts: localhost
connection: local
gather_facts: false
vars:
filetree_controller_settings: &filetree_create_output_dir "{{ filetree_create_output_dir }}"
filetree_controller_organizations: *filetree_create_output_dir
filetree_controller_labels: *filetree_create_output_dir
filetree_controller_user_accounts: *filetree_create_output_dir
filetree_controller_teams: *filetree_create_output_dir
filetree_controller_credential_types: *filetree_create_output_dir
filetree_controller_credentials: *filetree_create_output_dir
filetree_controller_credential_input_sources: *filetree_create_output_dir
filetree_controller_notifications: *filetree_create_output_dir
filetree_controller_projects: *filetree_create_output_dir
filetree_controller_execution_environments: *filetree_create_output_dir
filetree_controller_applications: *filetree_create_output_dir
filetree_controller_inventories: *filetree_create_output_dir
filetree_controller_inventory_sources: *filetree_create_output_dir
filetree_controller_instance_groups: *filetree_create_output_dir
filetree_controller_hosts: *filetree_create_output_dir
filetree_controller_groups: *filetree_create_output_dir
filetree_controller_templates: *filetree_create_output_dir
filetree_controller_workflow_job_templates: *filetree_create_output_dir
filetree_controller_schedules: *filetree_create_output_dir
filetree_controller_roles: *filetree_create_output_dir
roles:
- infra.controller_configuration.filetree_read
post_tasks:
- name: "Create the output flatten dir"
ansible.builtin.file:
path: "{{ filetree_create_output_dir }}_flatten"
state: directory

- name: "Write all the objects to the corresponding file"
ansible.builtin.copy:
dest: "{{ filetree_create_output_dir }}_flatten/{{ item.name }}.yaml"
content: |
---
{{ item.value | to_nice_yaml(indent=2) }}
...
loop:
- name: controller_settings
value: "{{ controller_settings }}"
- name: controller_organizations
value: "{{ controller_organizations }}"
- name: controller_labels
value: "{{ controller_labels }}"
- name: controller_user_accounts
value: "{{ controller_user_accounts }}"
- name: controller_teams
value: "{{ controller_teams }}"
- name: controller_credential_types
value: "{{ controller_credential_types }}"
- name: controller_credentials
value: "{{ controller_credentials }}"
- name: controller_credential_input_sources
value: "{{ controller_credential_input_sources }}"
- name: controller_notifications
value: "{{ controller_notifications }}"
- name: controller_projects
value: "{{ controller_projects }}"
- name: controller_execution_environments
value: "{{ controller_execution_environments }}"
- name: controller_applications
value: "{{ controller_applications }}"
- name: controller_inventories
value: "{{ controller_inventories }}"
- name: controller_inventory_sources
value: "{{ controller_inventory_sources }}"
- name: controller_instance_groups
value: "{{ controller_instance_groups }}"
- name: controller_hosts
value: "{{ controller_hosts }}"
- name: controller_groups
value: "{{ controller_groups }}"
- name: controller_templates
value: "{{ controller_templates }}"
- name: controller_workflow_job_templates
value: "{{ controller_workflow_job_templates | default([]) }}"
- name: controller_schedules
value: "{{ controller_schedules }}"
- name: controller_roles
value: "{{ controller_roles }}"
...
120 changes: 80 additions & 40 deletions roles/filetree_create/tasks/job_templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,85 @@
order_by: 'organization,id'
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the <ORGANIZATION_NAME> output directories for job templates in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
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_control:
loop_var: needed_path
label: "{{ __path }}"
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
block:
- name: "Add current job_templates to the job_templates flat file"
blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
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_id: "{{ current_job_templates_asset_value.id }}"
job_template_name: "{{ current_job_templates_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/job_templates.yaml"
query_labels: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.labels,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_error: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_error,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_started: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_started,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_success: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_success,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
last_job_template: "{{ current_job_template_index == ((job_templates_lookvar | length) - 1) }}"
loop: "{{ job_templates_lookvar }}"
loop_control:
index_var: current_job_template_index
loop_var: current_job_templates_asset_value
label: "{{ __dest }}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Add current job_templates to the <ORGANIZATION_NAME>/job_templates output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_job_templates.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ job_template_name | regex_replace('/', '_') }}.yaml"
query_labels: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.labels,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_error: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_error,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_started: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_started,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_success: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_success,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
loop: "{{ job_templates_lookvar }}"
loop_control:
loop_var: current_job_templates_asset_value
label: "{{ __dest }}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"
- 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 <ORGANIZATION_NAME> output directories for job templates in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
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_control:
loop_var: needed_path
label: "{{ __path }}"

- name: "Add current job_templates to the <ORGANIZATION_NAME>/job_templates output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_job_templates.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
job_template_organization: "{{ current_job_templates_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS') }}"
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 }}_{{ job_template_name | regex_replace('/', '_') }}.yaml"
query_labels: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.labels,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_error: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_error,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_started: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_started,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
query_notification_success: "{{ query(controller_api_plugin, current_job_templates_asset_value.related.notification_templates_success,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects) }}"
loop: "{{ job_templates_lookvar }}"
loop_control:
loop_var: current_job_templates_asset_value
label: "{{ __dest }}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"
...
52 changes: 39 additions & 13 deletions roles/filetree_create/tasks/team_roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,44 @@
}}"
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the output directory for team roles: {{ output_path }}"
ansible.builtin.file:
path: "{{ output_path }}/team_roles"
state: directory
mode: '0755'
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
block:
- name: "Stat if the output file exists"
ansible.builtin.stat:
path: "{{ output_path }}/team_roles.yaml"
register: team_roles_file

- name: "Add current roles to the output yaml file"
ansible.builtin.template:
src: "templates/current_team_roles.j2"
dest: "{{ output_path }}/team_roles/current_roles_{{ teamname | regex_replace('/', '_') }}.yaml"
mode: '0644'
vars:
current_team_roles_asset_value: "{{ team_roles_lookvar }}"
when: team_roles_lookvar | length > 0
- name: "Add current team roles to the team roles flat file"
blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_team_roles.j2') }}"
vars:
current_team_roles_asset_value: "{{ team_roles_lookvar }}"
__dest: "{{ output_path }}/team_roles.yaml"
first_team_role: "{{ not team_roles_file.stat.exists }}"
when: team_roles_lookvar | length > 0

- 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 team roles: {{ output_path }}"
ansible.builtin.file:
path: "{{ output_path }}/team_roles"
state: directory
mode: '0755'

- name: "Add current roles to the output yaml file"
ansible.builtin.template:
src: "templates/current_team_roles.j2"
dest: "{{ output_path }}/team_roles/current_roles_{{ teamname | regex_replace('/', '_') }}.yaml"
mode: '0644'
vars:
current_team_roles_asset_value: "{{ team_roles_lookvar }}"
when: team_roles_lookvar | length > 0
...
85 changes: 57 additions & 28 deletions roles/filetree_create/tasks/teams.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,71 @@
order_by: 'organization,id'
no_log: "{{ controller_configuration_filetree_create_secure_logging }}"

- name: "Create the <ORGANIZATION_NAME>/teams output directory for teams in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/teams"
loop: "{{ (teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"
- name: "Block for to generate flatten output"
when:
- flatten_output is defined
- flatten_output | bool
block:
- name: "Add current teams to the teams flat file"
blockinfile:
create: true
insertafter: EOF
path: "{{ __dest }}"
marker: ""
block: "{{ lookup('template', 'templates/current_teams.j2') }}"
vars:
team_organization: "{{ (current_teams_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}"
team_id: "{{ current_teams_asset_value.id }}"
team_name: "{{ current_teams_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/teams.yaml"
last_team: "{{ current_team_index == ((teams_lookvar | length) - 1) }}"
loop: "{{ teams_lookvar }}"
loop_control:
index_var: current_team_index
loop_var: current_teams_asset_value
label: "{{ __dest }}"

- name: "Add current teams to the <ORGANIZATION_NAME>/teams output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_teams.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
team_organization: "{{ (current_teams_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}"
team_id: "{{ current_teams_asset_value.id }}"
team_name: "{{ current_teams_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ team_organization | regex_replace('/', '_') }}/teams/{{ team_id }}_{{ team_name | regex_replace('/', '_') }}.yaml"
loop: "{{ teams_lookvar }}"
loop_control:
loop_var: current_teams_asset_value
label: "{{ __dest }}"
- 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 <ORGANIZATION_NAME>/teams output directory for teams in {{ output_path }}"
ansible.builtin.file:
path: "{{ __path }}"
state: directory
mode: '0755'
vars:
__path: "{{ output_path }}/{{ needed_path | regex_replace('/', '_') }}/teams"
loop: "{{ (teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'defined') | map(attribute='organization') | map(attribute='name') | list | flatten | unique)
+ (['ORGANIZATIONLESS'] if ((teams_lookvar | map(attribute='summary_fields') | selectattr('organization', 'undefined') | list | flatten) | length > 0) else [])
}}"
loop_control:
loop_var: needed_path
label: "{{ __path }}"

- name: "Add current teams to the <ORGANIZATION_NAME>/teams output file in {{ output_path }}"
ansible.builtin.template:
src: "templates/current_teams.j2"
dest: "{{ __dest }}"
mode: '0644'
vars:
team_organization: "{{ (current_teams_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}"
team_id: "{{ current_teams_asset_value.id }}"
team_name: "{{ current_teams_asset_value.name | regex_replace('/', '_') }}"
__dest: "{{ output_path }}/{{ team_organization | regex_replace('/', '_') }}/teams/{{ team_id }}_{{ team_name | regex_replace('/', '_') }}.yaml"
loop: "{{ teams_lookvar }}"
loop_control:
loop_var: current_teams_asset_value
label: "{{ __dest }}"

- name: "Set the team's roles"
ansible.builtin.include_tasks: "team_roles.yml"
vars:
team_organization: "{{ (current_teams_asset_value.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}"
team_organization: "{{ (current_team.summary_fields.organization.name | default('ORGANIZATIONLESS', true)) | regex_replace('/', '_') }}"
teamname: "{{ current_team.name }}"
teamid: "{{ current_team.id }}"
last_team_role: "{{ current_team_index_for_roles == ((teams_lookvar | length) - 1) }}"
loop: "{{ teams_lookvar }}"
loop_control:
index_var: current_team_index_for_roles
loop_var: current_team
...
Loading

0 comments on commit 0a83891

Please sign in to comment.