I need to replace a specific string inside a file with the value of an environment variable, but I only get and empty value.
Here my code:
- template:
src: myfile.yml
dest: /etc/myfile.yml
mode: '0755'
- name: Update template env
become: true
shell: sed -i "s/HELK_ID/{{ lookup('env', 'HELK_ID') }}/" /etc/myfile.yml
Inside /etc/environment:
HELK_ID=aaaa
How can I replace the string with the environment value?
2
Answers
Since Lookup plugins
the
env
lookup – Read the value of environment variablesTo gather the environment on the Remote Node it is recommended to
gather_facts
about theenv
. The answer under Is it possible to gather only specific facts in Ansible? show also how to debug.After that tasks like
are possible.
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
read the file
and use the filter community.general.jc
gives
Given the file
Use the module replace to substitute the environment variable
gives, running with ‘–check –diff’ options
Example of a complete playbook for testing
gives