You can use following methodes to set env variables directly inside hosts
-
lineinfile
tasks: - name: Set env variable lineinfile: path: {{ user_bashrc_path }}/.bashrc line: export MY_VAR=test - name: Test env variable shell: . {{ user_bashrc_path }}/.bashrc && echo $MY_VAR register: mytestenv - debug: var=mytestenv
-
blockinfile
tasks: - name: Set env variables blockinfile: dest: {{ user_bashrc_path }}/.bashrc block: | export {{ item.name }}={{ item.line }} marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}" with_items: - { name: test1, line: hello1 } - { name: test2, line: hello2 } - { name: test3, line: hello3 } - name: Test env shell: . {{ user_bashrc_path }}/.bashrc && echo $test1 register: mytestenv - debug: var=mytestenv