Update examples in documentation for env lookup plugin (#62662)

This commit is contained in:
Jiri Tyr 2020-01-23 20:27:30 +00:00 committed by Sam Doran
parent 9e8fb5b7f5
commit 913037731f

View file

@ -8,34 +8,51 @@ DOCUMENTATION = """
lookup: env lookup: env
author: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com> author: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com>
version_added: "0.9" version_added: "0.9"
short_description: read the value of environment variables short_description: Read the value of environment variables
description: description:
- Allows you to query the environment variables available on the controller when you invoked Ansible. - Allows you to query the environment variables available on the
controller when you invoked Ansible.
options: options:
_terms: _terms:
description: Environment variable or list of them to lookup the values for description:
- Environment variable or list of them to lookup the values for.
required: True required: True
notes:
- The module returns an empty string if the environment variable is not
defined. This makes it impossbile to differentiate between the case the
variable is not defined and the case the variable is defined but it
contains an empty string.
- The C(default) filter requires second parameter to be set to C(True)
in order to set a default value in the case the variable is not
defined (see examples).
""" """
EXAMPLES = """ EXAMPLES = """
- debug: msg="{{ lookup('env','HOME') }} is an environment variable" - name: Basic usage
debug:
msg: "'{{ lookup('env', 'HOME') }}' is the HOME environment variable."
- name: Example how to set default value if the variable is not defined
debug:
msg: "'{{ lookup('env', 'USR') | default('nobody', True) }}' is the user."
""" """
RETURN = """ RETURN = """
_list: _list:
description: description:
- values from the environment variables. - Values from the environment variables.
type: list type: list
""" """
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
from ansible.utils import py3compat from ansible.utils import py3compat
class LookupModule(LookupBase): class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs): def run(self, terms, variables, **kwargs):
ret = [] ret = []
for term in terms: for term in terms:
var = term.split()[0] var = term.split()[0]
ret.append(py3compat.environ.get(var, '')) ret.append(py3compat.environ.get(var, ''))