Control node environment & plugins

40 views
Skip to first unread message

Gabriel Canton

unread,
May 15, 2022, 6:19:36 PM5/15/22
to ansible...@googlegroups.com
Hi community! I have a simple, newbie question related to setting the environment for plugins. This playbook illustrates my problem:

---
- name: env behaviour
  hosts: localhost
  gather_facts: no
  environment:
    MY_VAR: foo

  tasks:
    - name: shell w/environment
      shell: echo $MY_VAR
      environment:
        MY_VAR: bar

    - name: env lookup w/environment
      debug:
        msg: "{{ lookup('ansible.builtin.env', 'MY_VAR') }}"
      environment:
        MY_VAR: bar

As documented, although the environment keyword will work in the first task, the lookup is not aware of the env value set for MY_VAR, either at the play or task level. Now, is there any way to set the environment that will work for plugins as well? I have a lookup plugin that retrieves a token from the environment (calling os.getenv/os.environ.get), and I'd like to be able to set it at run time instead of hardcoding it in a shell variable.
Thanks,

Gabriel

Brian Coca

unread,
May 19, 2022, 9:33:57 AM5/19/22
to Ansible Project
The 'environment' keyword ONLY applies to the modules, all other
lookups share the controller environment and cannot be modified
inline.

--
----------
Brian Coca

Gabriel Canton

unread,
May 21, 2022, 2:18:04 PM5/21/22
to Ansible Project
Thanks for your answer Brian! So you  say there's no way? It doesn't have to be inline in the task itself. If there's some task that can modify the controller environment beforehand that's good enough...

Brian Coca

unread,
May 24, 2022, 9:58:18 AM5/24/22
to Ansible Project
no such facility exists, each task runs in a fork so even if you
change the environment successfully with a action plugin, it would not
persist for the next task.

--
----------
Brian Coca

Gabriel Canton

unread,
May 24, 2022, 6:09:38 PM5/24/22
to ansible...@googlegroups.com
I was kind of expecting something like that... It's a pity, but thanks a lot for clarifying this!

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/AZnB_uwMKoU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7cSCZeATGg%2BwVEU%3DiWhYA7zG_kHkoOvCyGx5Pm1HLrjPA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages