# Environment variable

**URL:** <https://forum.ansible.com/t/environment-variable/38977>\
**Category:** Get Help\
**Created:** [December 8, 2024, 9:31am UTC](https://forum.ansible.com/t/environment-variable/38977 "2024-12-08T09:31:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [December 8, 2024, 9:31am UTC](https://forum.ansible.com/t/environment-variable/38977/1 "2024-12-08T09:31:13Z")

</div>

Hello,

I’m trying to reset the ANSIBLE\_LOG\_PATH environment variable. To do this, I’ve written the following code:

play.yml

```auto
- name: test la genration de fichier de log
  hosts: all
  #gather_facts: no
  connection: local
  tasks:
    - import_role:
        name: role1

```

role1/main.yml

```auto
---

- set_fact:
    datetime: "{{ ansible_date_time.iso8601_basic_short }}"

- name: "Emplacement du log"
  block:
    - file:
        path: "{{ inventory_dir }}/logs"
        state: directory
    - file:
        path: "{{ inventory_dir }}/logs/{{ datetime }}_{{ ansible_role_name }}.log"
        state: touch
  delegate_to: localhost

- name: 
  command: export $ANSIBLE_LOG_PATH
  environment:
    ANSIBLE_LOG_PATH: "{{ inventory_dir }}/logs/{{ datetime }}_{{ ansible_role_name }}.log"

- block:
  - shell: echo $ANSIBLE_LOG_PATH
    register: test
  - name: Affichage de la variable test
    debug:
      msg: "{{ test }}"

- name: Affichage de la variable role1
  debug:
    msg: "{{ role1 }}"

```

I get the following error without understanding why

```auto
TASK [role1 : command] *****************************************************************************************************************************************************************************************
fatal: [ma_machine1]: FAILED! => {"changed": false, "cmd": "export /home/alexandre/inventory/logs/20241208T100856_role1.log", "msg": "[Errno 2] No such file or directory: b'export'", "rc": 2, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

```

However, the file is present in the logs directory.

I’d like to reload this variable in order to change the log file when a role is started.

Thank You

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [December 8, 2024, 12:06pm UTC](https://forum.ansible.com/t/environment-variable/38977/2 "2024-12-08T12:06:42Z")

</div>

`export` is a shell builtin and you’re using the `command` module (which doesn’t use a shell). This will never work.

Even if you used the `shell` module it wouldn’t work as the export would happen within that shell invocation and not carried forward.

At best (though I don’t know whether log output can be reset) you use the `environment:` task attribute on the task you want the log to be changed.

---

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [December 8, 2024, 2:38pm UTC](https://forum.ansible.com/t/environment-variable/38977/3 "2024-12-08T14:38:14Z")

</div>

Thanks for your feedback, I must admit that when I tried to use copilot, I got the following library:

```auto
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
from ansible.plugins.callback import CallbackBase

class CallbackModule(CallbackBase):
    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'notification'
    CALLBACK_NAME = 'role_logger'
    CALLBACK_NEEDS_WHITELIST = True

    def __init__ (self, display=None):
        super(CallbackModule, self). __init__ (display)
        self.logs = {}
        self.current_role = None
        self.log_dir = os.getenv('ANSIBLE_LOG_DIR', '/path/to/logs')

    def v2_playbook_on_task_start(self, task, is_conditional):
        role = task._role
        if role:
            self.current_role = role.get_name()
            if self.current_role not in self.logs:
                self.logs[self.current_role] = []

    def v2_runner_on_ok(self, result):
        self._log(result)

    def v2_runner_on_failed(self, result, ignore_errors=False):
        self._log(result)

    def v2_runner_on_skipped(self, result):
        self._log(result)

    def v2_runner_on_unreachable(self, result):
        self._log(result)

    def _log(self, result):
        if self.current_role:
            msg = self._dump_results(result._result)
            self.logs[self.current_role].append(msg)
            self._write_log(self.current_role)

    def v2_playbook_on_stats(self, stats):
        pass # No need to handle logs here, already handled per task.

    def _write_log(self, role_name):
        log_file = os.path.join(self.log_dir, f"log_{role_name}_{self._timestamp()}.log")
        with open(log_file, 'w') as f:
            for entry in self.logs[role_name]:
                f.write(entry + '\n')

    def _timestamp(self):
        import datetime
        return datetime.datetime.now().strftime('%Y%m%d%H%M%S')

```

I placed it in a directory at the root of my play. But I didn’t know how to use it, even though I had set the variables in ansible.cfg:

```auto
[defaults]
callback_whitelist = role_logger
callback_plugins = ./callback_plugins

```

I had no log files being created. Could you help me to get this library working?  
Where to place it?  
How to call it?  
…  
Thanking you

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [December 9, 2024, 4:17pm UTC](https://forum.ansible.com/t/environment-variable/38977/4 "2024-12-09T16:17:46Z")

</div>

That is a custom callback plugin, but you can just use existing ones that create log files, here are a few:

[syslog\_json](https://docs.ansible.com/ansible/latest/collections/community/general/syslog_json_callback.html)  
[tree](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/tree_callback.html)  
[log plays](https://docs.ansible.com/ansible/latest/collections/community/general/log_plays_callback.html)
