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

Use ansible-runner instead of ansible-playbook #17688

Merged
merged 7 commits into from
Jul 17, 2018

Conversation

Ladas
Copy link
Contributor

@Ladas Ladas commented Jul 10, 2018

Use ansible-runner instead of ansible-playbook

Depends on:

Implements:
https://bugzilla.redhat.com/show_bug.cgi?id=1599798

@Ladas
Copy link
Contributor Author

Ladas commented Jul 10, 2018

@miq-bot assign @agrare

cc @Fryguy @gtanzillo

JSON.parse(result.output)
end

def prepare_tmp_structure
base_dir = "/tmp/ansible-runner/"
mkdir(base_dir)
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to use Dir.mktmpdir here and just delete the whole directory after we're done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe? Not sure, I was thinking to just put it to /tmp and the OS will take care of cleaning it.

@Ladas
Copy link
Contributor Author

Ladas commented Jul 13, 2018

@Fryguy @agrare @gtanzillo ok, so I did a workaround for the output. So now it's in a usable shape. So we just need ansible-runner 1.0.4 on the appliance and we can merge. (with 2 minor bugs in ansible-runner, that will clean up the code a bit when fixed)

end

def mkdir(base_dir)
Dir.mkdir(base_dir) unless Dir.exist?(base_dir)
Copy link
Member

Choose a reason for hiding this comment

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

Can you just rescue EEXIST and save the conditional? Saves a stat() system call 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I will just keep this, with hope that I can delete this once the mentioned bug is fixed in runner :-) ansible/ansible-runner#88

But you are right, before I was sharing the dir for many runs, so it would be faster, now I shouldn't even need it, since new empty temp dir is always created

result = AwesomeSpawn.run!(ansible_command, :env => env_vars, :params => [{:extra_vars => JSON.dump(extra_vars)}, playbook_path])
JSON.parse(result.output)
Dir.mktmpdir("ansible-runner") do |base_dir|
mkdir(base_dir + '/project') # without this, there is a silent fail of the ansible-runner command see https://github.com/ansible/ansible-runner/issues/88
Copy link
Member

Choose a reason for hiding this comment

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

File.join would be better than + '/'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will fix

parsed_stdout << JSON.parse(line)
rescue => e
_log.warn("Couldn't parse JSON from: #{e}")
end
Copy link
Member

Choose a reason for hiding this comment

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

I can see this being a good method by itself if we want to parse&return stdout and stderr but we can extract this when/if we go with that.

Use mktmpdir to clean up ansible-runner files after the run
Deal with output which is JSON per line and for now, deal with
possible non json lines.
Add a result object so we can easily extend it in the future
Return integer return code, with possibility that the rc file
is missing.

result = AwesomeSpawn.run!(ansible_command(base_dir), :env => env_vars, :params => [{:cmdline => "--extra-vars '#{JSON.dump(extra_vars)}'", :playbook => playbook_path}])

return Ansible::Runner::Response.new(:return_code => return_code(base_dir), :stdout => result.output, :stderr => result.error)
Copy link
Member

Choose a reason for hiding this comment

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

Do you need the return here? I think it will actually throw an exception:

>> a = Dir.mktmpdir("test") { |base_dir| return base_dir + "hello" }
LocalJumpError: unexpected return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see the exception but I can remove it

@return_code = return_code
@stdout = stdout
@parsed_stdout = parse_stdout(stdout)
@stderr = stderr
Copy link
Member

Choose a reason for hiding this comment

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

Is stderr not going to be json encoded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we might do that later, right now it always returns "", so I am not sure what the format will be. Might be the same as the stdout

Copy link
Member

Choose a reason for hiding this comment

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

👍

Remove extra return and reformat
@miq-bot
Copy link
Member

miq-bot commented Jul 17, 2018

Checked commits Ladas/manageiq@e214bf2~...5e9738f with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 2 offenses detected

lib/ansible/runner.rb

lib/ansible/runner/response.rb

@agrare agrare merged commit 5e9738f into ManageIQ:master Jul 17, 2018
agrare added a commit that referenced this pull request Jul 17, 2018
Use ansible-runner instead of ansible-playbook
@agrare agrare added this to the Sprint 91 Ending Jul 30, 2018 milestone Jul 17, 2018
@simaishi
Copy link
Contributor

@Ladas I see this is merged and the BZ is in POST. We don't need ansible-runner RPM?

@agrare
Copy link
Member

agrare commented Jul 23, 2018

@simaishi we do still need the RPM, it wasn't necessary for this PR but you are right that the BZ should have depended on having the RPM installed.

@Ladas
Copy link
Contributor Author

Ladas commented Jul 23, 2018

@simaishi it does, but it is not in production, so people testing this are just installing it manually via pip (where the 1.0.4 is already released)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants