1

I have the env file which is present in target machine and it contain certain number of variables with export command(the export command itself was present inside the file).

export AB_HOME=/et/dev/abinitio/abinitio-V3
export PATH=${AB_HOME}/bin:${PATH}

I have executed the env file using the below playbook and I tried to read the variables which are exported using the output1 which is a register variable in my playbook. But I am able to see my register variable is empty. Is there any way to get the variables which are all exported. I don't know the variables name which are present inside the file, So I am not able to use the ECHO command.

- hosts: dev
  gather_facts: false
  tasks:   
    - name: get the environment variables
      shell: "su <id> & . ./.env"
      args:
        chdir: /path to the file
      register: output1

    - debug: var=output1.stdout_lines 
Newbee2
  • 179
  • 1
  • 1
  • 6
  • You may want to check the exit status instead of stdout_lines: https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#rc – lainatnavi Feb 20 '20 at 08:33
  • Does that `shell: "su & . ./.env"` work? After running the playbook can you see the exported variables from bash (not with ansible)? I cannot source files in my ansible 2.0.0.2 installation. None of the listed solutions worked for me: https://stackoverflow.com/questions/22256884/not-possible-to-source-bashrc-with-ansible – lainatnavi Feb 20 '20 at 20:52
  • I am not able to get what you are asking, I manually tired the . ./.env command in the remote and I am able to see the exported variable – Newbee2 Feb 21 '20 at 07:46

1 Answers1

0

stdout for export is not returned, that is why you don't see any output. You may want to check the rc code instead:

- debug: var=output1.rc

To make ansible print the previously exported variables just run export or export -p and print stdout_lines in the same way as before.

tasks:   
    - name: get the environment variables
      shell: "export -p"
      register: output

    - debug: var=output.stdout_lines
lainatnavi
  • 1,453
  • 1
  • 14
  • 22
  • I am able to find rc output as zero – Newbee2 Feb 20 '20 at 09:21
  • you can use the value to check for successful completion. If your goal is printing the exported variable you can run `export`, register the output and then debug `output.std_lines` – lainatnavi Feb 20 '20 at 09:28
  • Yes my goal is to print the exported variable, but the question is output.std_lines does not have any content after the file executed. – Newbee2 Feb 20 '20 at 09:43
  • if you run `export x=somevalue` you won't get any stdout. It is explained in the answer. Those two lines `export AB_HOME=/et/dev/abinitio/abinitio-V3 export PATH=${AB_HOME}/bin:${PATH}` will not write on stdout, that is why `output1.stdout_lines` is empty. – lainatnavi Feb 20 '20 at 11:01
  • Then how to see or how to use them in environment keyword, output1.rc is also empty. I am not able to use echo, since I dont know what are all the variables inside the file, as it may change from one system to another. – Newbee2 Feb 20 '20 at 11:04
  • Just checked, that also providing the remote env variables, not the exported environment variables – Newbee2 Feb 20 '20 at 11:23
  • So you need to check with ansible what variables were exported after sourcing `.env`? – lainatnavi Feb 20 '20 at 11:40
  • Yes, I need that part only – Newbee2 Feb 20 '20 at 11:44