On Mon, 16 Oct 2023 at 09:09, Y.G Kumar <ygkuma...@gmail.com> wrote:

> This is not working either:
>

Attempting to read between the lines, you've misunderstood what the
"environment" parameter does.  The "environment" parameter sets the shell
environment variables for a specific *task*[1].  It doesn't affect later
tasks in the play.  The fragments you posted both attempt to set the
environment variable for a single task based on the future output of said
task.  Ansible isn't prescient, and can't do this.  Hence you see the
error.  What you probably wanted to do was retrieve the output, and then
use it in a future task.

Based on your response to Avinash you're trying to consume that environment
variable inside "pure.yml", which could be achieved as follows.

"""
- 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 }}"

    - include_tasks: pure.yml
      environment:
            MY_DISK_COUNT: "{{ disk_count }}"
"""

That said, I suspect you're trying to convert a bash script into an Ansible
playbook.  In which case "set_fact" is the closest analogue to setting
environment variables, and you're probably best totally ignoring the
"environment" parameter.  Once you use "set_fact" in a task, all tasks
after it can access the fact it sets.

For example:

"""
- hosts: localhost
  tasks:
    - name: "Get number of disks"
      shell: lsblk --nodeps  | grep disk | wc -l
      register: disk_count_result

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

    - name: "Do something with disk_count"
      shell: "echo {{ disk_count }}"

    - name: "Do something with MY_DISK_COUNT exposed as an environment
variable to this specific task"
      shell: "export | grep COUNT"
      environment:
        MY_DISK_COUNT: "{{ disk_count }}"

    - name: "repeat same command without exposing MY_DISK_COUNT to this
specific task"
      shell: "export | grep COUNT"
"""

However, as you appear to already know.  The question you've posted is
*not* what this list is for:

- It's not a bug in Ansible (which should be reported in GitHub as an
issue, but might get traction here)
- It's not an attempt to start a discussion about how some future feature
might function.
- It's not a question about developing a custom module for Ansible.
- It's not an announcement about a milestone in the Ansible development
lifecycle.

This kind of question is best suited for the ansible-project mailing list
which "is for sharing Ansible tips, answering questions about playbooks and
roles, and general user discussion".  But, you should include more than
just the output of a single task and its error.  I would also strongly
recommend spending some time working through some Ansible tutorials.  I
suspect you've misunderstood some of the underlying fundamentals of how
Ansible works, which is only going to lead to further frustration.


Mark
[1] In the case of block, import_tasks and include_tasks, it applies to
tasks run as part of the block/import/include.
-- 
Mark Chappell
Senior Principal Systems Engineer, Red Hat GmbH
Tel: +49 89 205 071 284   Mob: +49 172 7 32 16 87
Fax: +49 89 205 071 111
GnuPG: 6FEA E991 09E8 6CA2 0498  8696 D9E0 55E6 C46E A90E

Red Hat GmbH <https://www.redhat.com/de/global/dach>,
Sitz: Werner von Siemens Ring 12, D-85630 Grasbrunn
Handelsregister: Amtsgericht München, HRB 153243,
Geschäftsführer: Ryan Barnhart, Charles Cachera, Michael O'Neill, Amy Ross

-- 
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/CAMCBCy%2B3p2Ex7FP5%2BisyjsLjQRxqUBKTd48XeBkgqi%3DZF1xnuA%40mail.gmail.com.

Reply via email to