Skip to content

Commit

Permalink
Fixes #38219 - fix Ansible Tower API path based on AAP version
Browse files Browse the repository at this point in the history
  • Loading branch information
nofaralfasi committed Feb 24, 2025
1 parent 71fc34b commit 8b33b97
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ description: |
#!/bin/sh

echo "Calling Ansible AWX/Tower provisioning callback..."

# Attempt to use the /api/controller/v2 API for AAP 2.5 and higher. If successful, continue with newer API endpoints.
response=$(/usr/bin/curl -v -k -s --write-out "%{http_code}" -o /dev/null https://<%= host_param('ansible_tower_fqdn') %>/api/controller/v2/)

# If the /api/controller/v2 API fails, fallback to /api/v2 for older AAP versions.
if [ "$response" -eq 200 ]; then
api_base='/api/controller/v2/job_templates/' # Use newer API for AAP 2.5+
else
api_base='/api/v2/job_templates/' # Fallback to older API versions
fi

<% if host_param('ansible_extra_vars') -%>
/usr/bin/curl -v -k -s -H 'Content-Type: application/json' --data '{"host_config_key":"<%= host_param('ansible_host_config_key') %>", "extra_vars": <%=host_param('ansible_extra_vars') %>}' https://<%= host_param('ansible_tower_fqdn') %>/api/v2/job_templates/<%= host_param('ansible_job_template_id') %>/callback/
/usr/bin/curl -v -k -s -H 'Content-Type: application/json' --data '{"host_config_key":"<%= host_param('ansible_host_config_key') %>", "extra_vars": <%= host_param('ansible_extra_vars') %>}' https://<%= host_param('ansible_tower_fqdn') %>${api_base}<%= host_param('ansible_job_template_id') %>/callback/
<% else -%>
/usr/bin/curl -v -k -s --data "host_config_key=<%= host_param('ansible_host_config_key') %>" https://<%= host_param('ansible_tower_fqdn') %>/api/v2/job_templates/<%= host_param('ansible_job_template_id') %>/callback/
/usr/bin/curl -v -k -s --data "host_config_key=<%= host_param('ansible_host_config_key') %>" https://<%= host_param('ansible_tower_fqdn') %>${api_base}<%= host_param('ansible_job_template_id') %>/callback/
<% end -%>
echo "DONE"
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ description: |
The content of a systemd service for running Ansible Tower / AWX callback script one-time.
Used in the ansible_provisioning_callback snippet for systemd enabled systems.
-%>

# Attempt to use the /api/controller/v2 API for AAP 2.5 and higher. If successful, continue with newer API endpoints.
response=$(/usr/bin/curl -v -k -s --write-out "%{http_code}" -o /dev/null https://<%= host_param('ansible_tower_fqdn') %>/api/controller/v2/)

# If the /api/controller/v2 API fails, fallback to /api/v2 for older AAP versions.
if [ "$response" -eq 200 ]; then
api_base='/api/controller/v2/job_templates/' # Use newer API for AAP 2.5+
else
api_base='/api/v2/job_templates/' # Fallback to older API versions
fi

[Unit]
Description=Provisioning callback to Ansible Tower
Wants=network-online.target
Expand All @@ -15,9 +26,9 @@ After=network-online.target
[Service]
Type=oneshot
<% if host_param('ansible_extra_vars') -%>
ExecStart=/usr/bin/curl -k -s -H 'Content-Type: application/json' --data '{"host_config_key":"<%= host_param('ansible_host_config_key') %>", "extra_vars": <%=host_param('ansible_extra_vars') %>}' https://<%= host_param('ansible_tower_fqdn') %>/api/v2/job_templates/<%= host_param('ansible_job_template_id') %>/callback/
ExecStart=/usr/bin/curl -k -s -H 'Content-Type: application/json' --data '{"host_config_key":"<%= host_param('ansible_host_config_key') %>", "extra_vars": <%=host_param('ansible_extra_vars') %>}' https://<%= host_param('ansible_tower_fqdn') %>${api_base}<%= host_param('ansible_job_template_id') %>/callback/
<% else -%>
ExecStart=/usr/bin/curl -k -s --data "host_config_key=<%= host_param('ansible_host_config_key') -%>" https://<%= host_param('ansible_tower_fqdn') -%>/api/v2/job_templates/<%= host_param('ansible_job_template_id') -%>/callback/
ExecStart=/usr/bin/curl -k -s --data "host_config_key=<%= host_param('ansible_host_config_key') -%>" https://<%= host_param('ansible_tower_fqdn') -%>${api_base}<%= host_param('ansible_job_template_id') -%>/callback/
<% end -%>
ExecStartPost=/usr/bin/systemctl disable ansible-callback

Expand Down

0 comments on commit 8b33b97

Please sign in to comment.