You can run a shell task, echo the variables, register the result, and
examine that. Otherwise:
*[utoddl@tango ansible]$ cat ygkumar0.yml*
---
# Playbook ygkumar0.yml
- name: Test environment variables in included tasks file
hosts: localhost
gather_facts: false
environment:
script_var1: hi
script_var2: there
tasks:
- name: Run tasks file
ansible.builtin.include_tasks:
file: tasks/ygkumar0.yml
*[utoddl@tango ansible]$ cat tasks/ygkumar0.yml*
---
# tasks/ygkumar0.yml
- name: Test environment variables by shell script
ansible.builtin.shell: |
echo "We're here."
echo "script_var0: '${script_var0-missing}'"
echo "script_var1: '${script_var1-missing}'"
echo "script_var2: '${script_var2-missing}'"
echo "script_var3: '${script_var3-missing}'"
- name: Examine env vars directly
/# THIS WON'T WORK! ansible.builtin.env allows you to query # the
environment variables available on the controller when you invoked
Ansible, # not the environment that tasks on the remote nodes see./
ansible.builtin.debug:
msg:
- "script_var0: {{ lookup('ansible.builtin.env', 'script_var0:') |
default('missing') }}"
- "script_var1: {{ lookup('ansible.builtin.env', 'script_var1:') |
default('missing') }}"
- "script_var2: {{ lookup('ansible.builtin.env', 'script_var2:') |
default('missing') }}"
- "script_var3: {{ lookup('ansible.builtin.env', 'script_var3:') |
default('missing') }}"
*[utoddl@tango ansible]$ ansible-playbook ygkumar0.yml -vv*
ansible-playbook [core 2.14.10]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/utoddl/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.11/site-packages/ansible
ansible collection location =
/home/utoddl/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible-playbook
python version = 3.11.5 (main, Aug 28 2023, 00:00:00) [GCC 13.2.1 20230728
(Red Hat 13.2.1-1)] (/usr/bin/python3)
jinja version = 3.0.3
libyaml = True
Using /etc/ansible/ansible.cfg as config file
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
PLAYBOOK: ygkumar0.yml
*****************************************************************************
1 plays in ygkumar0.yml
PLAY [Test environment variables in included tasks file]
*******************************************
TASK [Run tasks file]
******************************************************************************
task path: /home/utoddl/ansible/ygkumar0.yml:10
included: /home/utoddl/ansible/tasks/ygkumar0.yml for localhost
TASK [Test environment variables by shell script]
**************************************************
task path: /home/utoddl/ansible/tasks/ygkumar0.yml:3
changed: [localhost] => changed=true
cmd: |-
echo "We're here."
echo "script_var0: '${script_var0-missing}'"
echo "script_var1: '${script_var1-missing}'"
echo "script_var2: '${script_var2-missing}'"
echo "script_var3: '${script_var3-missing}'"
delta: '0:00:00.002717'
end: '2023-10-12 11:12:48.601248'
msg: ''
rc: 0
start: '2023-10-12 11:12:48.598531'
stderr: ''
stderr_lines: <omitted>
stdout: |-
We're here.
script_var0: 'missing'
script_var1: 'hi'
script_var2: 'there'
script_var3: 'missing'
stdout_lines: <omitted>
TASK [Examine env vars directly]
*******************************************************************
task path: /home/utoddl/ansible/tasks/ygkumar0.yml:11
ok: [localhost] =>
msg:
- 'script_var0: '
- 'script_var1: '
- 'script_var2: '
- 'script_var3: '
PLAY RECAP
*****************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
On 10/11/23 10:03 AM, Y.G Kumar wrote:
Hi All,
I am declaring an environment variables in a playbook as shown below:
--
---
- hosts: localhost
environment:
script_var1: hi
script_var2: there
tasks:
- name: List
shell: bash test.sh
--
I want to test the existence of the above environment variables in a
separate task.yml file. How do I test their existence ? What is the
syntax ?
--
You received this message because you are subscribed to the Google
Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/1e7a8869-6360-4396-95e6-cd6f078dcd24n%40googlegroups.com
<https://groups.google.com/d/msgid/ansible-project/1e7a8869-6360-4396-95e6-cd6f078dcd24n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
Todd
--
You received this message because you are subscribed to the Google Groups "Ansible
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/b59b1d83-bede-4f8c-af7e-8332a2f4b0ca%40gmail.com.