You can't expect the module shell will provide you by default with the environment variables from /etc/environment. The file /etc/environment is used by PAM. If you need it, it's easier to read and parse the file on your own. For example, given the file
shell> cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
HELK_ID=aaaa
read the file
- command: cat /etc/environment
register: env_out
and use the filter community.general.jc
env: "{{ env_out.stdout|community.general.jc('ini') }}"
gives
env:
HELK_ID: aaaa
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Given the file
shell> cat /tmp/myfile.yml
var1: [foo, HELK_ID, bar]
Use the module replace to substitute the environment variable
- replace:
path: /tmp/myfile.yml
regexp: 'HELK_ID'
replace: "{{ env.HELK_ID }}"
gives, running with '--check --diff' options
TASK [replace] *******************************************************************************
--- before: /tmp/myfile.yml
+++ after: /tmp/myfile.yml
@@ -1 +1 @@
-var1: [foo, HELK_ID, bar]
+var1: [foo, aaaa, bar]
Example of a complete playbook for testing
- hosts: all
vars:
env: "{{ env_out.stdout|community.general.jc('ini') }}"
tasks:
- command: cat /etc/environment
register: env_out
check_mode: false # runs the command also with --check
- debug:
var: env
- replace:
path: /tmp/myfile.yml
regexp: 'HELK_ID'
replace: "{{ env.HELK_ID }}"
gives
shell> ansible-playbook pb.yml -l localhost --check --diff
PLAY [all] ***********************************************************************************
TASK [command] *******************************************************************************
changed: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
env:
HELK_ID: aaaa
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
TASK [replace] *******************************************************************************
--- before: /tmp/myfile.yml
+++ after: /tmp/myfile.yml
@@ -1 +1 @@
-var1: [foo, HELK_ID, bar]
+var1: [foo, aaaa, bar]
changed: [localhost]
PLAY RECAP ***********************************************************************************
localhost: ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0