diff --git a/bin/ansible-runner-environment.sh b/bin/ansible-runner-environment.sh new file mode 100755 index 0000000..320397c --- /dev/null +++ b/bin/ansible-runner-environment.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +[ -f "$SMART_PROXY_ANSIBLE_ENVIRONMENT_FILE" ] && source "$SMART_PROXY_ANSIBLE_ENVIRONMENT_FILE" +exec "$@" diff --git a/lib/smart_proxy_ansible/plugin.rb b/lib/smart_proxy_ansible/plugin.rb index 24ef5a8..9ea4b80 100644 --- a/lib/smart_proxy_ansible/plugin.rb +++ b/lib/smart_proxy_ansible/plugin.rb @@ -5,7 +5,8 @@ class Plugin < Proxy::Plugin rackup_path File.expand_path('http_config.ru', __dir__) settings_file 'ansible.yml' plugin :ansible, Proxy::Ansible::VERSION - default_settings :ansible_dir => Dir.home + default_settings :ansible_dir => Dir.home, + :ansible_environment_file => '/etc/foreman-proxy/ansible.env' # :working_dir => nil load_classes ::Proxy::Ansible::ConfigurationLoader diff --git a/lib/smart_proxy_ansible/runner/ansible_runner.rb b/lib/smart_proxy_ansible/runner/ansible_runner.rb index afb6b9f..76764c0 100644 --- a/lib/smart_proxy_ansible/runner/ansible_runner.rb +++ b/lib/smart_proxy_ansible/runner/ansible_runner.rb @@ -10,6 +10,9 @@ class AnsibleRunner < ::Proxy::Dynflow::Runner::Parent include ::Proxy::Dynflow::Runner::ProcessManagerCommand attr_reader :execution_timeout_interval + # To make this overridable in development + ENVIRONMENT_WRAPPER = ENV['SMART_PROXY_ANSIBLE_ENVIRONMENT_WRAPPER'] || '/usr/libexec/foreman-proxy/ansible-runner-environment' + def initialize(input, suspended_action:, id: nil) super input, :suspended_action => suspended_action, :id => id @inventory = rebuild_secrets(rebuild_inventory(input), input) @@ -186,10 +189,12 @@ def write_ssh_key def start_ansible_runner env = {} env['FOREMAN_CALLBACK_DISABLE'] = '1' if @rex_command - command = [env, 'ansible-runner', 'run', @root, '-p', 'playbook.yml'] + env['SMART_PROXY_ANSIBLE_ENVIRONMENT_FILE'] = Proxy::Ansible::Plugin.settings[:ansible_environment_file] + command = ['ansible-runner', 'run', @root, '-p', 'playbook.yml'] command << '--cmdline' << cmdline unless cmdline.nil? command << verbosity if verbose? - initialize_command(*command) + + initialize_command(env, ENVIRONMENT_WRAPPER, *command) logger.debug("[foreman_ansible] - Running command '#{command.join(' ')}'") end diff --git a/settings.d/ansible.yml.example b/settings.d/ansible.yml.example index 0540bf4..c702ad3 100644 --- a/settings.d/ansible.yml.example +++ b/settings.d/ansible.yml.example @@ -1,3 +1,4 @@ --- :enabled: true :working_dir: '~/.foreman-ansible' +# :ansible_environment_file: /etc/foreman-proxy/ansible.env diff --git a/smart_proxy_ansible.gemspec b/smart_proxy_ansible.gemspec index 5022c1a..431a6c4 100644 --- a/smart_proxy_ansible.gemspec +++ b/smart_proxy_ansible.gemspec @@ -19,7 +19,8 @@ Gem::Specification.new do |gem| 'LICENSE', 'README.md', 'lib/smart_proxy_ansible.rb', 'lib/smart_proxy_ansible/**/*', - 'bin/json_inventory.sh'] + 'bin/json_inventory.sh', + 'bin/ansible-runner-environment.sh'] gem.extra_rdoc_files = ['README.md', 'LICENSE'] gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ['lib']