diff --git a/changelogs/fragments/issue_743.yml b/changelogs/fragments/issue_743.yml index 584f53e76..c9748473e 100644 --- a/changelogs/fragments/issue_743.yml +++ b/changelogs/fragments/issue_743.yml @@ -1,3 +1,4 @@ --- bugfixes: - - Avoid the hosts populated by a constructed inventory to be removed + - Organization not defined when exporting some inventory sources from Tower 3.7.2 + - Constructed inventories can only be exported when AAP version is >= 4.5.0 diff --git a/roles/filetree_create/tasks/all.yml b/roles/filetree_create/tasks/all.yml index 3fd1f2a50..fa7f105fa 100644 --- a/roles/filetree_create/tasks/all.yml +++ b/roles/filetree_create/tasks/all.yml @@ -1,24 +1,14 @@ --- -- name: "Get the Authentication Token for the future requests" - ansible.builtin.uri: - url: "https://{{ controller_hostname }}/api/v2/tokens/" - user: "{{ controller_username }}" - password: "{{ controller_password }}" - method: POST - force_basic_auth: true - validate_certs: "{{ controller_validate_certs }}" - status_code: 201 - register: authtoken_res - -- name: "Set the oauth token to be used since now" +- name: "Get the Tower/AAP instance version" ansible.builtin.set_fact: - oauthtoken: "{{ authtoken_res.json.token }}" - oauthtoken_url: "{{ authtoken_res.json.url }}" - no_log: true - + aap_version: "{{ lookup(controller_api_plugin, 'ping', + host=controller_hostname, oauth_token=controller_oauthtoken, + verify_ssl=controller_validate_certs).version }}" - name: "Check if the connection is to an Ansible Tower or to Automation Platform" ansible.builtin.set_fact: - is_aap: "{{ lookup(controller_api_plugin, 'ping', host=controller_hostname, oauth_token=oauthtoken, verify_ssl=controller_validate_certs).version is version('4.0.0', '>=') }}" + is_aap: "{{ aap_version is version('4.0.0', '>=') }}" + have_constructed: "{{ aap_version is version('4.5.0', '>=') }}" + no_log: "{{ controller_configuration_filetree_create_secure_logging }}" - name: "Block to get the organization_filter ID to filter all the queries" when: @@ -46,7 +36,7 @@ when: "'inventory' in input_tag or 'all' in input_tag" - name: "Export Constructed Inventories" ansible.builtin.include_tasks: "constructed_inventory.yml" - when: "'inventory' in input_tag or 'all' in input_tag" + when: "('inventory' in input_tag or 'all' in input_tag) and have_constructed" - name: "Export Credentials" ansible.builtin.include_tasks: "credentials.yml" when: "'credentials' in input_tag or 'all' in input_tag" diff --git a/roles/filetree_create/templates/current_inventory_sources.j2 b/roles/filetree_create/templates/current_inventory_sources.j2 index 3ff63d941..1395f04d4 100644 --- a/roles/filetree_create/templates/current_inventory_sources.j2 +++ b/roles/filetree_create/templates/current_inventory_sources.j2 @@ -5,22 +5,52 @@ controller_inventory_sources: {% for inventory_source in current_inventory_sources_asset_value %} - name: "{{ inventory_source.name }}" description: "{{ inventory_source.description }}" +{% if inventory_source.summary_fields.organization is defined %} + organization: "{{ inventory_source.summary_fields.organization.name }}" +{% endif %} source: "{{ inventory_source.source | default('ToDo: The source of the inventory_source was originally missing and must be specified',true) }}" -{% if inventory_source.source_project %} +{% if inventory_source.source_project is defined and inventory_source.source_project != None %} source_project: "{{ inventory_source.summary_fields.source_project.name }}" {% endif %} -{% if inventory_source.source_path %} +{% if inventory_source.source_path is defined %} source_path: "{{ inventory_source.source_path }}" {% endif %} -{% if inventory_source.source_vars and inventory_source.source_vars != '---' %} +{% if inventory_source.source_vars is defined and inventory_source.source_vars != '---' and inventory_source.source_vars != '' %} source_vars: {{ inventory_source.source_vars | from_yaml | to_nice_yaml(indent=2) | indent(width=6, first=False) | replace("'{{", "!unsafe \'{{") }} {%- endif %} inventory: "{{ inventory_source.summary_fields.inventory.name }}" update_on_launch: "{{ inventory_source.update_on_launch }}" overwrite: "{{ inventory_source.overwrite }}" -{% if inventory_source.credential %} - credential: "{{ inventory_source.credential }}" +{% if inventory_source.credential is defined %} + credential: "{{ inventory_source.summary_fields.credential.name }}" +{% endif %} +{% set query_notification_error = query(controller_api_plugin, inventory_source.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) %} +{% set query_notification_started = query(controller_api_plugin, inventory_source.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) %} +{% set query_notification_success = query(controller_api_plugin, inventory_source.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) %} +{% if query_notification_error | length > 0 %} + notification_templates_error: +{% for notification_error in query_notification_error %} + - "{{ notification_error.name }}" +{% endfor %} +{% endif %} +{% if query_notification_started | length > 0 %} + notification_templates_started: +{% for notification_started in query_notification_started %} + - "{{ notification_started.name }}" +{% endfor %} +{% endif %} +{% if query_notification_success | length > 0 %} + notification_templates_success: +{% for notification_success in query_notification_success %} + - "{{ notification_success.name }}" +{% endfor %} {% endif %} {% endfor %} {% if last_inventory | default(true) | bool %}