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

Fixes #35456 - Source environment before starting ansible-runner #66

Merged
merged 5 commits into from Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/ansible-runner-environment.sh
@@ -0,0 +1,4 @@
#!/bin/sh
ehelms marked this conversation as resolved.
Show resolved Hide resolved

[ -f "$ANSIBLE_ENVIRONMENT_FILE" ] && source "$ANSIBLE_ENVIRONMENT_FILE"
exec "$@"
3 changes: 2 additions & 1 deletion lib/smart_proxy_ansible/plugin.rb
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/smart_proxy_ansible/runner/ansible_runner.rb
Expand Up @@ -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/ansible-runner-environment'
adamruzicka marked this conversation as resolved.
Show resolved Hide resolved

def initialize(input, suspended_action:, id: nil)
super input, :suspended_action => suspended_action, :id => id
@inventory = rebuild_secrets(rebuild_inventory(input), input)
Expand Down Expand Up @@ -186,10 +189,12 @@ def write_ssh_key
def start_ansible_runner
env = {}
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in theforeman/puppet-foreman_proxy#777 (comment) I wonder if we should present everything as environment variables by default. In that case I'd probably extract the env var construction to a separate method.

env['FOREMAN_CALLBACK_DISABLE'] = '1' if @rex_command
ekohl marked this conversation as resolved.
Show resolved Hide resolved
command = [env, 'ansible-runner', 'run', @root, '-p', 'playbook.yml']
env['ANSIBLE_ENVIRONMENT_FILE'] = Proxy::Ansible::Plugin.settings[:ansible_environment_file]
Copy link
Member

Choose a reason for hiding this comment

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

Is ANSIBLE_ENVIRONMENT_FILE an Ansible configuration value or something we are making up? I ask because if this is something we are making up we should probably avoid using the ANSIBLE_ prefix as that is a "known" pattern amongst Ansible configuration values when provided via environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is something I made up

Copy link
Member

Choose a reason for hiding this comment

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

A thought to avoid potential issues there would be to use your prefix from above SMART_PROXY_ANSIBLE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me SMART_PROXY_ANSIBLE_ENVIRONMENT_FILE reads like it is a file that should contain environment for smart-proxy-ansible, not ansible spawned by smart-proxy-ansible, but sure, why not

Copy link
Member

Choose a reason for hiding this comment

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

To me SMART_PROXY_ANSIBLE_ENVIRONMENT_FILE reads like it is a file that should contain environment for smart-proxy-ansible, not ansible spawned by smart-proxy-ansible, but sure, why not

Fair. Naming is hard. Maybe @evgeni has an opinion as our resident expert on all things "fun" that can get us in trouble with Ansible.

Copy link
Member

Choose a reason for hiding this comment

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

I think SMART_PROXY_ANSIBLE_ is a good prefix for config options for the smart_proxy_ansible plugin. In a container you'd probably do the same thing for all options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

Done

Did you mean to push up new changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Erm, yes, sorry. It is out now

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

Expand Down
1 change: 1 addition & 0 deletions settings.d/ansible.yml.example
@@ -1,3 +1,4 @@
---
:enabled: true
:working_dir: '~/.foreman-ansible'
# :ansible_environment_file: /etc/foreman-proxy/ansible.env