Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #32030, #32188 - process events with unknown hosts #398

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/foreman_ansible_core/runner/ansible_runner.rb
Expand Up @@ -52,9 +52,9 @@ def handle_event_file(event_file)
logger.debug("[foreman_ansible] - parsing event file #{event_file}")
begin
event = JSON.parse(File.read(event_file))
if (hostname = event.dig('event_data', 'host'))
if hostname_for_event(event)
handle_host_event(hostname, event)
else
elsif event.key?('stdout')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we have a play which runs on hosts A and B and which has a task which gets delegated to localhost for both, would this mean that outputs of both hosts will contain outputs for both delegated tasks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed, that's what would happen. I can try to investigate if there is some other indication as for what host this is running FOR, but unless I have any luck with that, I don't see much of better ways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've seen, if a task is delegated, then .event_data.res has a _ansible_delegated_vars key, otherwise it is missing. Although I'm not sure how much we can rely on this.

handle_broadcast_data(event)
end
true
Expand All @@ -64,6 +64,13 @@ def handle_event_file(event_file)
end
end

def hostname_for_event(event)
hostname = event.dig('event_data', 'host') || event.dig('event_data', 'remote_addr')
return nil if hostname.blank? || hostname == 'locahost'
raise "handle_host_event: unknown host #{hostname}" unless @targets.key?(hostname)
hostname
end

def handle_host_event(hostname, event)
log_event("for host: #{hostname.inspect}", event)
publish_data_for(hostname, event['stdout'] + "\n", 'stdout') if event['stdout']
Expand Down