Skip to content

Commit

Permalink
Move logic to fetch galaxy roles to Ansible::Runner
Browse files Browse the repository at this point in the history
This is needed because the roles directory location is located
relative to the playbook, not always the root of the repo.

This will allow us to handle a case where a repo is configured with
the following directory structure:

azure_servers/
  roles/
    azure_stuffs/
    requirements.yml
  azure_playbook.yml
aws_servers/
  roles/
    s3_stuffs/
    ec2_config/
  aws_playbook.yml
  s3_playbook.yml
localhost/
  requirements.yml
  localhost_playbook.yml

Where previously we would not have installed any roles because we
wouldn't have found a /roles directory at the top-level of the
repo directory.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1734904
  • Loading branch information
carbonin committed Jul 31, 2019
1 parent 2aba2be commit 6bfcc74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def playbooks_in_git_repository
def checkout_git_repository(target_directory)
git_repository.update_repo
git_repository.checkout(scm_branch, target_directory)
Ansible::Content.new(target_directory).fetch_galaxy_roles
end

ERROR_MAX_SIZE = 50.kilobytes
Expand Down
8 changes: 8 additions & 0 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def run_via_cli(hosts, credentials, env_vars, extra_vars, tags: nil, ansible_run
params = runner_params(base_dir, ansible_runner_method, playbook_or_role_args, verbosity)

begin
fetch_galaxy_roles(playbook_or_role_args)
result = AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params)
res = response(base_dir, ansible_runner_method, result)
ensure
Expand Down Expand Up @@ -292,6 +293,13 @@ def validate_params!(env_vars, extra_vars, tags, ansible_runner_method, playbook
raise ArgumentError, errors.join("; ") if errors.any?
end

def fetch_galaxy_roles(playbook_or_role_args)
return unless playbook_or_role_args[:playbook]

playbook_dir = File.dirname(playbook_or_role_args[:playbook])
Ansible::Content.new(playbook_dir).fetch_galaxy_roles
end

def credentials_info(credentials, base_dir)
command_line = {}
env_vars = {}
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/ansible/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
expect(File.exist?(File.join(dir, "env", "cmdline"))).to be_falsey
end.and_return(result)

expect_galaxy_roles_fetched

described_class.run(env_vars, extra_vars, playbook)
end

Expand All @@ -56,6 +58,8 @@
expect(cmdline).to eq("--tags #{tags}")
end.and_return(result)

expect_galaxy_roles_fetched

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

Expand Down Expand Up @@ -105,6 +109,8 @@
expect(extravars).to eq("name" => "john's server", "ansible_connection" => "local")
end.and_return(result)

expect_galaxy_roles_fetched

described_class.run(env_vars, extra_vars, playbook)
end
end
Expand Down Expand Up @@ -137,6 +143,8 @@
expect(File.exist?(File.join(dir, "env", "cmdline"))).to be_falsey
end.and_return(result)

expect_galaxy_roles_fetched

runner_result = described_class.run_async(env_vars, extra_vars, playbook)
expect(runner_result).kind_of?(Ansible::Runner::ResponseAsync)
end
Expand Down Expand Up @@ -257,4 +265,10 @@
expect(MiqQueue.first.zone).to eq(zone.name)
end
end

def expect_galaxy_roles_fetched
content_double = instance_double(Ansible::Content)
expect(Ansible::Content).to receive(:new).with("/path/to/my").and_return(content_double)
expect(content_double).to receive(:fetch_galaxy_roles)
end
end

0 comments on commit 6bfcc74

Please sign in to comment.