Environment variables

693 views
Skip to first unread message

Y.G Kumar

unread,
Oct 15, 2023, 1:43:13 PM10/15/23
to Ansible Development
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

Avinash Jadhav

unread,
Oct 15, 2023, 1:47:01 PM10/15/23
to Y.G Kumar, Ansible Development
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 }}"


--
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.

Y.G Kumar

unread,
Oct 16, 2023, 3:09:21 AM10/16/23
to Ansible Development
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"}

Y.G Kumar

unread,
Oct 16, 2023, 8:06:25 AM10/16/23
to Ansible Development
Just to reiterate my requirement.  I just want to create a remote environment variable by assigning to it the value contained in a registered variable while executing the shell command
as shown below....

---
- hosts: localhost

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

    - name: "set fact"
      environment:
            MY_DISK_COUNT: "{{ disk_count_result.stdout }}"

Mark Chappell

unread,
Oct 16, 2023, 8:32:29 AM10/16/23
to Y.G Kumar, Ansible Development
On Mon, 16 Oct 2023 at 09:09, Y.G Kumar <ygku...@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

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
Reply all
Reply to author
Forward
0 new messages