Skip to content

Commit

Permalink
POC: lookup plugin references playbook variables
Browse files Browse the repository at this point in the history
  • Loading branch information
john-odonnell committed Apr 16, 2024
1 parent cb3facc commit 1a06ff4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
42 changes: 41 additions & 1 deletion plugins/lookup/conjur_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
key: config_file_path
env:
- name: CONJUR_CONFIG_FILE
sample_key:
description: Sample
type: string
default: someDefault
required: False
ini:
- section: conjur,
key: sample_key
vars:
- name: sample_key
env:
- name: SAMPLE_KEY
"""

EXAMPLES = """
Expand Down Expand Up @@ -272,7 +284,35 @@ def run(self, terms, variables=None, **kwargs):
elif not terms[0] or terms[0].isspace():
raise AnsibleError("Invalid secret path: empty secret path not accepted.")

self.set_options(direct=kwargs)
# Variables belonging to the parent playbook, including those set via
# the --extra-vars flag on a `ansible-playbook` call, are available to
# LookupModule class as the `variables` parameter passed to the `run`
# entrypoint
display.display("POC LOGS: variables parameter type: " + str(type(variables)))
try:
display.display("POC LOGS: variable sample_key value: " + variables["sample_key"])
except KeyError:
display.display("POC LOGS: sample_key not in variables dictionary")

# We should register the variables as LookupModule options.
#
# Doing this has some nice advantages if we're considering supporting
# a set of Ansible variables that could sometimes replace environment
# variables.
#
# Registering the variables as options forces them to adhere to the
# behavior described in the DOCUMENTATION variable. An option can have
# both a Ansible variable and environment variable source, which means
# Ansible will do some juggling on our behalf.
self.set_options(var_options=variables, direct=kwargs)
display.display("POC LOGS: plugin option sample_key present: " + str(self.has_option("sample_key")))

# The method `self.get_option` will:
# 1. return the value of the Ansible variable sample_key, or
# 2. return the value of the environment variable SAMPLE_KEY, or
# 3. either use a specified default or throw an error if option required
display.display("POC LOGS: variable sample_key from options: " + self.get_option("sample_key"))

validate_certs = self.get_option('validate_certs')
conf_file = self.get_option('config_file')
as_file = self.get_option('as_file')
Expand Down
1 change: 1 addition & 0 deletions tests/conjur_variable/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function run_test_case {
fi
# You can add -vvvvv here for debugging
export SAMPLE_KEY="set_in_env"
ansible-playbook 'test_cases/${test_case}/playbook.yml'
py.test --junitxml='./junit/${test_case}' \
Expand Down

0 comments on commit 1a06ff4

Please sign in to comment.