Skip to content

Commit

Permalink
fixes filetree_create not exporting constructed inventories when AAP …
Browse files Browse the repository at this point in the history
…< 2.4. Fixed Organization field check (redhat-cop#754)

* fixes filetree_create not exporting constructed inventories when AAP < 2.4. Fixed Organization field check

* fixes filetree_create not exporting constructed inventories when AAP < 2.4. Fixed Organization field check

* fixes filetree_create not exporting constructed inventories when AAP < 2.4. Fixed Organization field check
  • Loading branch information
ivarmu authored and Przemyslaw Kalitowski committed Nov 22, 2024
1 parent f8f3c56 commit e67a559
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
3 changes: 2 additions & 1 deletion changelogs/fragments/issue_743.yml
Original file line number Diff line number Diff line change
@@ -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
26 changes: 8 additions & 18 deletions roles/filetree_create/tasks/all.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"
Expand Down
40 changes: 35 additions & 5 deletions roles/filetree_create/templates/current_inventory_sources.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down

0 comments on commit e67a559

Please sign in to comment.