This is not working either:

---
---
- hosts: localhost
  tasks:
    - name: "subtask"
      shell: lsblk --nodeps  | grep disk | wc -l
      register: disk_count_result

    - name: "set fact"
      set_fact:
        disk_count: "{{ disk_count_result.stdout | int }}"
      environment:
            MY_DISK_COUNT: "{{ disk_count }}"

    - include_tasks: pure.yml
---

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': 
line 8, column 7, but may\nbe elsewhere in the file depending on the exact 
syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"set 
fact\"\n      ^ here\n"}

On Sunday, October 15, 2023 at 11:17:01 PM UTC+5:30 avinash...@gmail.com 
wrote:

> Please try this one
>
> ---
> hosts: localhost
> tasks:
>   - name: "subtask"
>     command: lsblk --nodeps | grep disk | wc -l
>     register: disk_count_result
>
>   - name: "set fact"
>     set_fact:
>       disk_count: "{{ disk_count_result.stdout | int }}"
>
>   - name: "print disk count"
>     debug:
>       msg: "Disk count is {{ disk_count }}"
>
>   - name: "use disk count in environment variable"
>     shell: echo "Disk Count: {{ disk_count }}"
>     environment:
>       MY_DISK_COUNT: "{{ disk_count }}"
>
>
> On Sun, 15 Oct, 2023, 23:13 Y.G Kumar, <ygku...@gmail.com> wrote:
>
>> Hi All,
>>
>> Please don't yell at me for posting in this group.
>> I have already written to the ansible project group.
>> They are saying it is not possible to achieve the following result.
>> I have written the below code and it is not working.
>>
>> ----
>> hosts: localhost
>>   tasks:
>>     - name: "subtask"
>>       shell: lsblk --nodeps  | grep disk | wc -l
>>       register: disk_count
>>       environment: "{{ disk_count.stdout }}"
>> -- 
>>
>> It is throwing undefined variable disk_count. I want to capture the value 
>> of
>> the shell command output into an environment variable. But it is not 
>> working.
>> Can someone help me achieve this ?
>>
>> Thanks
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Development" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to ansible-deve...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-devel/235199ad-4ac5-4928-92a1-70512b098cf8n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-devel/235199ad-4ac5-4928-92a1-70512b098cf8n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-devel/4c3e3eb3-0f28-4dd7-81a9-b7d6775995b9n%40googlegroups.com.

Reply via email to