Hi All,
Thanks for the responses. I am facing another issue with env variables.
I want to register a variable to capture the output of a shell command as
shown below. So far its fine..
But I also want to save that command result in a remote environment
variable and that is not working. My playbook is as follows:
----
hosts: localhost
tasks:
- name: "subtask"
shell: lsblk --nodeps | grep disk | wc -l
register: disk_count
environment: "{{ disk_count.stdout }}"
--
--
fatal: [localhost]: FAILED! => {"msg": "The field 'environment' has an
invalid value, which includes an undefined variable. The error was:
'disk_count' is undefined\n\nThe error appears to be in '/root/test.yaml'
---
How do I have to workaround this for capturing the shell command value into
an environment variable ? Please suggest..
On Friday, October 13, 2023 at 5:33:43 PM UTC+5:30 Todd Lewis wrote:
> 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/441c225f-87d2-4716-940c-a36996c9c6dfn%40googlegroups.com.