Skip to content

Commit

Permalink
Allow skip inventory related objects (#7)
Browse files Browse the repository at this point in the history
* feat: allow export only inventory

* fix: missing dashes

* fix: remove the blocks

---------

Co-authored-by: Przemyslaw Kalitowski <[email protected]>
  • Loading branch information
przemkalit and Przemyslaw Kalitowski authored Oct 17, 2024
1 parent 90714f8 commit f82a1d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- filetree_create is able to export inventory without sources/hosts/groups.
...
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
bugfixes:
- filetree_create job_template double quote issue
...
3 changes: 3 additions & 0 deletions roles/filetree_create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ The following variables are required for that role to work properly:
| `organization`| N/A | no | str | Default organization for all objects that have not been set in the source controller.|
| `export_related_objects` | False | no | bool | Whether to export related objects (job templates related to certain workflows and the projects associated with these job templates) when a single JT or a single WFJT are being exported. |
| `update_project_state` | False | no | bool | Whether the project should be updated after import to the target controller. |
| `skip_inventory_sources` | False | no | bool | Whether the inventory sources should be exported with inventory. |
| `skip_inventory_hosts` | False | no | bool | Whether the inventory hosts should be exported with inventory. |
| `skip_inventory_groups` | False | no | bool | Whether the inventory groups should be exported with inventory. |

## Dependencies

Expand Down
4 changes: 3 additions & 1 deletion roles/filetree_create/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ output_path: "/tmp/filetree_output"
# Maximum number of objects to return from the list. If a list view returns more an max_objects an exception will be raised
query_controller_api_max_objects: 10000
export_related_objects: false

skip_inventory_sources: false
skip_inventory_hosts: false
skip_inventory_groups: false
controller_configuration_filetree_create_secure_logging: "{{ controller_configuration_secure_logging | default('false') }}"

input_tag:
Expand Down
39 changes: 20 additions & 19 deletions roles/filetree_create/tasks/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@

- name: "Set the inventory's inventory sources"
ansible.builtin.include_tasks: "inventory_sources.yml"
when: current_inventory_sources.total_inventory_sources > 0
when: current_inventory_sources.total_inventory_sources > 0 and not skip_inventory_sources
vars:
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)
else
output_path + '/inventory_sources.yaml' }}"
if (flatten_output is not defined or (flatten_output | bool) == false)
else
output_path + '/inventory_sources.yaml' }}"
current_inventory_sources_asset_value: "{{ query(controller_api_plugin, current_inventory_sources.related.inventory_sources,
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects)
if current_inventory_sources.has_inventory_sources else []
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects)
if current_inventory_sources.has_inventory_sources else []
}}"
last_inventory: "{{ current_inventory_index == ((inventory_lookvar | length) - 1) }}"
loop: "{{ inventory_lookvar }}"
Expand All @@ -113,18 +113,19 @@

- name: "Set the inventory's hosts"
ansible.builtin.include_tasks: "hosts.yml"
when: not skip_inventory_hosts
vars:
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)
else
output_path + '/hosts.yaml' }}"
if (flatten_output is not defined or (flatten_output | bool) == false)
else
output_path + '/hosts.yaml' }}"
current_hosts_asset_value: "{{ query(controller_api_plugin, current_inventory_hosts.related.hosts,
query_params={'not__description': 'imported'},
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects)
if not current_inventory_hosts.has_inventory_sources else []
query_params={'not__description': 'imported'},
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects)
if not current_inventory_hosts.has_inventory_sources else []
}}"
loop: "{{ inventory_lookvar }}"
loop_control:
Expand All @@ -135,20 +136,20 @@

- name: "Set the inventory's groups"
ansible.builtin.include_tasks: "groups.yml"
when: current_inventory_groups.total_groups > 0
when: current_inventory_groups.total_groups > 0 and not skip_inventory_groups
vars:
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)
else
output_path + '/groups.yaml' }}"
if (flatten_output is not defined or (flatten_output | bool) == false)
else
output_path + '/groups.yaml' }}"
current_groups_asset_value: "{{ query(controller_api_plugin, current_inventory_groups.related.groups,
query_params={'not__description': 'imported'},
host=controller_hostname, oauth_token=controller_oauthtoken, verify_ssl=controller_validate_certs,
return_all=true, max_objects=query_controller_api_max_objects)
if (not current_inventory_groups.has_inventory_sources or current_inventory_groups.kind is match('smart')) else []
}}"
}}"
loop: "{{ inventory_lookvar }}"
loop_control:
index_var: current_inventory_index
Expand Down

0 comments on commit f82a1d2

Please sign in to comment.