env: update examples for setting default values to variable (#83370)

pull/81594/merge
krescent 4 months ago committed by GitHub
parent f261a6142f
commit 46ff7594a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -30,22 +30,21 @@ EXAMPLES = """
ansible.builtin.debug: ansible.builtin.debug:
msg: "'{{ lookup('ansible.builtin.env', 'HOME') }}' is the HOME environment variable." msg: "'{{ lookup('ansible.builtin.env', 'HOME') }}' is the HOME environment variable."
- name: Before 2.13, how to set default value if the variable is not defined. - name: Before 2.13, how to set default value if the variable is not defined
This cannot distinguish between USR undefined and USR=''.
ansible.builtin.debug: ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.env', 'USR')|default('nobody', True) }} is the user." msg: "Hello {{ lookup('ansible.builtin.env', 'UNDEFINED_VARIABLE') | default('World', True) }}"
- name: Example how to set default value if the variable is not defined, ignores USR='' - name: Example how to set default value if the variable is not defined
ansible.builtin.debug: ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.env', 'USR', default='nobody') }} is the user." msg: "Hello {{ lookup('ansible.builtin.env', 'UNDEFINED_VARIABLE', default='World') }}"
- name: Set default value to Undefined, if the variable is not defined - name: Fail if the variable is not defined by setting default value to 'Undefined'
ansible.builtin.debug: ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.env', 'USR', default=Undefined) }} is the user." msg: "Hello {{ lookup('ansible.builtin.env', 'UNDEFINED_VARIABLE', default=Undefined) }}"
- name: Set default value to undef(), if the variable is not defined - name: Fail if the variable is not defined by setting default value to 'undef()'
ansible.builtin.debug: ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.env', 'USR', default=undef()) }} is the user." msg: "Hello {{ lookup('ansible.builtin.env', 'UNDEFINED_VARIABLE', default=undef()) }}"
""" """
RETURN = """ RETURN = """

Loading…
Cancel
Save