Skip to content

Commit

Permalink
Merge pull request #19118 from carbonin/make_cred_modules_accessible
Browse files Browse the repository at this point in the history
Run ansible-runner with PYTHONPATH set to access additional modules
  • Loading branch information
Fryguy authored Aug 9, 2019
2 parents ee2db85 + 50829f0 commit 3bedecb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def run_via_cli(hosts, credentials, env_vars, extra_vars, tags: nil, ansible_run
end
command_line_hash.merge!(cred_command_line)

env_vars_hash = env_vars.merge(cred_env_vars)
env_vars_hash = env_vars.merge(cred_env_vars).merge(python_env)
extra_vars_hash = extra_vars.merge(cred_extra_vars)

create_hosts_file(base_dir, hosts)
Expand Down Expand Up @@ -300,6 +300,19 @@ def fetch_galaxy_roles(playbook_or_role_args)
Ansible::Content.new(playbook_dir).fetch_galaxy_roles
end

def python_env
python3_modules_path = "/var/lib/awx/venv/ansible/lib/python3.6/site-packages/"
python2_modules_path = "/var/lib/manageiq/venv/lib/python2.7/site-packages/"

if File.exist?(python3_modules_path)
{ "PYTHONPATH" => python3_modules_path }
elsif File.exist?(python2_modules_path)
{ "PYTHONPATH" => python2_modules_path }
else
{}
end
end

def credentials_info(credentials, base_dir)
command_line = {}
env_vars = {}
Expand Down
32 changes: 32 additions & 0 deletions spec/lib/ansible/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@
described_class.run(env_vars, extra_vars, playbook, :become_enabled => true)
end

it "sets PYTHONPATH correctly with python3 modules installed " do
python2_modules_path = "/var/lib/manageiq/venv/lib/python2.7/site-packages/"
python3_modules_path = "/var/lib/awx/venv/ansible/lib/python3.6/site-packages/"

allow(File).to receive(:exist?).with(python2_modules_path).and_return(false)
allow(File).to receive(:exist?).with(python3_modules_path).and_return(true)

expect(AwesomeSpawn).to receive(:run) do |command, options|
expect(command).to eq("ansible-runner")

expect(options[:env]["PYTHONPATH"]).to eq(python3_modules_path)
end.and_return(result)

described_class.run(env_vars, extra_vars, playbook, :become_enabled => true)
end

it "sets PYTHONPATH correctly with python2 modules installed " do
python2_modules_path = "/var/lib/manageiq/venv/lib/python2.7/site-packages/"
python3_modules_path = "/var/lib/awx/venv/ansible/lib/python3.6/site-packages/"

allow(File).to receive(:exist?).with(python2_modules_path).and_return(true)
allow(File).to receive(:exist?).with(python3_modules_path).and_return(false)

expect(AwesomeSpawn).to receive(:run) do |command, options|
expect(command).to eq("ansible-runner")

expect(options[:env]["PYTHONPATH"]).to eq(python2_modules_path)
end.and_return(result)

described_class.run(env_vars, extra_vars, playbook, :become_enabled => true)
end

context "with special characters" do
let(:env_vars) { {"ENV1" => "pa$%w0rd!'"} }
let(:extra_vars) { {"name" => "john's server"} }
Expand Down

0 comments on commit 3bedecb

Please sign in to comment.