From 93bdb159f5fce856c8f84327296910163936ec1e Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Wed, 25 Jul 2018 14:06:31 +0200 Subject: [PATCH 1/5] Allow to run role without playbook Allow to run role without playbook --- lib/ansible/runner.rb | 63 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index 34dc2b1daa8..7694bffa429 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -2,7 +2,11 @@ module Ansible class Runner class << self def run(env_vars, extra_vars, playbook_path) - run_via_cli(env_vars, extra_vars, playbook_path) + run_via_cli(env_vars, extra_vars, :playbook_path => playbook_path) + end + + def run_role(env_vars, extra_vars, role_name, roles_path: nil, role_skip_facts: true) + run_via_cli(env_vars, extra_vars, :role_name => role_name, :roles_path => roles_path, :role_skip_facts => role_skip_facts) end def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) @@ -23,14 +27,17 @@ def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) private - def run_via_cli(env_vars, extra_vars, playbook_path) - validate_params!(env_vars, extra_vars, playbook_path) + def run_via_cli(env_vars, extra_vars, playbook_path: nil, role_name: nil, roles_path: nil, role_skip_facts: true) + validate_params!(env_vars, extra_vars, playbook_path, roles_path) Dir.mktmpdir("ansible-runner") do |base_dir| - result = AwesomeSpawn.run!(ansible_command(base_dir), - :env => env_vars, - :params => [{:cmdline => "--extra-vars '#{JSON.dump(extra_vars)}'", - :playbook => playbook_path}]) + result = AwesomeSpawn.run(ansible_command(base_dir), + :env => env_vars, + :params => params(:extra_vars => extra_vars, + :playbook_path => playbook_path, + :role_name => role_name, + :roles_path => roles_path, + :role_skip_facts => role_skip_facts)) Ansible::Runner::Response.new(:return_code => return_code(base_dir), :stdout => result.output, @@ -38,6 +45,40 @@ def run_via_cli(env_vars, extra_vars, playbook_path) end end + def params(extra_vars:, playbook_path:, role_name:, roles_path:, role_skip_facts:) + role_or_playbook_params = if playbook_path + playbook_params(:playbook_path => playbook_path) + elsif role_name + role_params(:role_name => role_name, + :roles_path => roles_path, + :role_skip_facts => role_skip_facts) + end + + [shared_params(:extra_vars => extra_vars).merge(role_or_playbook_params)] + end + + def playbook_params(playbook_path: playbook_path) + {:playbook => playbook_path} + end + + def role_params(role_name:, roles_path:, role_skip_facts:) + role_params = {:role => role_name} + + if roles_path + role_params[:"roles-path"] = roles_path + end + + if role_skip_facts + role_params[:"role-skip-facts"] = nil + end + + role_params + end + + def shared_params(extra_vars:) + {:cmdline => "--extra-vars '#{JSON.dump(extra_vars)}'"} + end + def return_code(base_dir) File.read(File.join(base_dir, "artifacts/result/rc")).to_i rescue @@ -45,10 +86,11 @@ def return_code(base_dir) 1 end - def validate_params!(env_vars, extra_vars, playbook_path) + def validate_params!(env_vars, extra_vars, playbook_path, roles_path) assert_hash!(env_vars) assert_hash!(extra_vars) assert_path!(playbook_path) + assert_path!(roles_path) end def assert_hash!(hash) @@ -58,14 +100,15 @@ def assert_hash!(hash) end def assert_path!(path) + return unless path + unless File.exist?(path) raise "File doesn't exist: #{path}" end end def ansible_command(base_dir) - # TODO add possibility to use custom path, e.g. from virtualenv - "ansible-runner run #{base_dir} --json -i result" + "ansible-runner run #{base_dir} --json -i result --hosts localhost" end end end From 44b6e6bc1db361cf37f4bd55a75b54c66785ee10 Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Wed, 25 Jul 2018 14:08:50 +0200 Subject: [PATCH 2/5] Allow to run run_role via queue Allow to run run_role via queue --- lib/ansible/runner.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index 7694bffa429..404c39ef4aa 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -10,11 +10,21 @@ def run_role(env_vars, extra_vars, role_name, roles_path: nil, role_skip_facts: end def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) + run_in_queue("run", env_vars, extra_vars, playbook_path, user_id, queue_opts) + end + + def run_role_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) + run_in_queue("run_role", env_vars, extra_vars, playbook_path, user_id, queue_opts) + end + + private + + def run_in_queue(method_name, env_vars, extra_vars, playbook_path, user_id, queue_opts) queue_opts = { :args => [env_vars, extra_vars, playbook_path], :queue_name => "generic", :class_name => name, - :method_name => "run", + :method_name => method_name, }.merge(queue_opts) task_opts = { @@ -25,8 +35,6 @@ def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) MiqTask.generic_action_with_callback(task_opts, queue_opts) end - private - def run_via_cli(env_vars, extra_vars, playbook_path: nil, role_name: nil, roles_path: nil, role_skip_facts: true) validate_params!(env_vars, extra_vars, playbook_path, roles_path) From 18f9abb5fa4d27ce328f42c1e101749a5542b3de Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Wed, 25 Jul 2018 15:18:29 +0200 Subject: [PATCH 3/5] Fix the queue methods args Fix the queue methods args --- lib/ansible/runner.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index 404c39ef4aa..d3492c3ad78 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -5,23 +5,26 @@ def run(env_vars, extra_vars, playbook_path) run_via_cli(env_vars, extra_vars, :playbook_path => playbook_path) end - def run_role(env_vars, extra_vars, role_name, roles_path: nil, role_skip_facts: true) + def run_role(env_vars, extra_vars, role_name, roles_path:, role_skip_facts: true) run_via_cli(env_vars, extra_vars, :role_name => role_name, :roles_path => roles_path, :role_skip_facts => role_skip_facts) end def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) - run_in_queue("run", env_vars, extra_vars, playbook_path, user_id, queue_opts) + run_in_queue("run", user_id, queue_opts, [env_vars, extra_vars, playbook_path]) end - def run_role_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts) - run_in_queue("run_role", env_vars, extra_vars, playbook_path, user_id, queue_opts) + def run_role_queue(env_vars, extra_vars, role_name, user_id, queue_opts, roles_path:, role_skip_facts: true) + run_in_queue("run_role", + user_id, + queue_opts, + [env_vars, extra_vars, role_name, {:roles_path => roles_path, :role_skip_facts => role_skip_facts}]) end private - def run_in_queue(method_name, env_vars, extra_vars, playbook_path, user_id, queue_opts) + def run_in_queue(method_name, user_id, queue_opts, args) queue_opts = { - :args => [env_vars, extra_vars, playbook_path], + :args => args, :queue_name => "generic", :class_name => name, :method_name => method_name, From 116946de119ac9652040fb772eb046f6ab70c390 Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Wed, 25 Jul 2018 17:29:50 +0200 Subject: [PATCH 4/5] Simplify the role param condition Simplify the role param condition --- lib/ansible/runner.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index d3492c3ad78..ebb1608a842 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -75,13 +75,8 @@ def playbook_params(playbook_path: playbook_path) def role_params(role_name:, roles_path:, role_skip_facts:) role_params = {:role => role_name} - if roles_path - role_params[:"roles-path"] = roles_path - end - - if role_skip_facts - role_params[:"role-skip-facts"] = nil - end + role_params[:"roles-path"] = roles_path if roles_path + role_params[:"role-skip-facts"] = nil if role_skip_facts role_params end From 00bd9beeef0ee6eefeb50859dcde065d14071a4f Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Wed, 25 Jul 2018 17:31:04 +0200 Subject: [PATCH 5/5] Fix circular argument reference Fix circular argument reference --- lib/ansible/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index ebb1608a842..ec2b236f7ed 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -68,7 +68,7 @@ def params(extra_vars:, playbook_path:, role_name:, roles_path:, role_skip_facts [shared_params(:extra_vars => extra_vars).merge(role_or_playbook_params)] end - def playbook_params(playbook_path: playbook_path) + def playbook_params(playbook_path:) {:playbook => playbook_path} end