Another thing you might consider is using a YAML reference to duplicate the environment in a regular playbook variable. Then you can use plain old variable references to see those values. See the example below:

*[utoddl@tango ansible]$ cat ygkumar1.yml *
---
# Playbook ygkumar1.yml
- name: Copy environment to testable variables using YAML reference
  hosts: localhost
  gather_facts: false
*environment: &environment script_var1: hi script_var2: there*
  vars:
    pb_dict:
      not_env_var1: foo
      not_env_var2: bar
      *env_vars: *environment*
      other_stuff: bas
  tasks:
    - name: Dump pb_dict to the job log
      ansible.builtin.debug:
        var: pb_dict

    - name: Shell script to loop over pb_dict.env_vars
      changed_when: false
      ansible.builtin.shell: |
        for vname in {{ pb_dict.env_vars.keys() | map('quote') | join(' ') }} ; 
do
            printf -- "Environment variable %s: %s\n" "$vname" "$(eval echo 
\${$vname} )"
        done
*[utoddl@tango ansible]$ ansible-playbook ygkumar1.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: ygkumar1.yml 
**********************************************************************
1 plays in ygkumar1.yml

PLAY [Copy environment to testable variables using YAML reference] 
**************************

TASK [Dump pb_dict to the job log] 
**********************************************************
task path: /home/utoddl/ansible/ygkumar1.yml:16
ok: [localhost] =>
  pb_dict:
    *env_vars: script_var1: hi script_var2: there*
    not_env_var1: foo
    not_env_var2: bar
    other_stuff: bas

TASK [Shell script to loop over pb_dict.env_vars] 
*******************************************
task path: /home/utoddl/ansible/ygkumar1.yml:20
ok: [localhost] => changed=false
  cmd: |-
    for vname in script_var1 script_var2 ; do
        printf -- "Environment variable %s: %s\n" "$vname" "$(eval echo \${$vname} 
)"
    done
  delta: '0:00:00.003708'
  end: '2023-10-13 07:58:40.898459'
  msg: ''
  rc: 0
  start: '2023-10-13 07:58:40.894751'
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    Environment variable script_var1: hi
    Environment variable script_var2: there
  stdout_lines: <omitted>

PLAY RECAP 
**********************************************************************************
localhost                  : ok=2    changed=0    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 [email protected]. 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b9e5d59f-3809-46b4-b360-2f8ad6114cee%40gmail.com.

Reply via email to