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.
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.
Please try this one
This is not working either:
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…
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:
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.