It might help if you can describe what you're trying to achieve more broadly.
Typically, if you're trying to do something complicated, and propagate state from task to task with loads of ansible.builtin.shell, you are likely to be approaching the task from the wrong angle. What Dick's tried to explain is that the environment keyword <https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_environment.html> used in the way you've used it in your example, sets the environment *for the task at the level it's running*. Not for future tasks, which seems to be what you want/expect. You presumably want to use disk_count.stdout in a subsequent task. So you would set disk_count.stdout in the *environment clause for a subsequent task*. (And you'd need to format it for use as an environment variable. Read the docs.) Ultimately though, if you're jumping through a bunch of hoops like this you probably want to step back from shell and environment variables and think about 1) what you're trying to achieve end-to-end and 2) how you might do that using native modules rather than shell/command.? On Sun, 15 Oct 2023 at 18:37, Y.G Kumar <ygkuma...@gmail.com> wrote: > So, you mean to say that we can't achieve this in ansible ? > > On Sunday, October 15, 2023 at 4:21:42 PM UTC+5:30 Dick Visser wrote: > >> On Sun, 15 Oct 2023 at 08:24, Y.G Kumar <ygku...@gmail.com> wrote: >> >>> 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' >>> >> >> This is because the expression in the environment parameter is evaluated >> before the task, and at that time it is not yet known. >> In general, if you need the output of a command to actually run the >> command, that seems like a circular dependency to me. >> Ansible will not help you fix that. >> >> >> -- > 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/795c8547-c6ca-44a5-beb0-59c50e483e42n%40googlegroups.com > <https://groups.google.com/d/msgid/ansible-project/795c8547-c6ca-44a5-beb0-59c50e483e42n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAKtKohTT8%2BNU3FqsRULRYHLsbg0y5aE4sVO0BYLNu8UAN3C1fA%40mail.gmail.com.