Thanks much for your reply, I think this is what the solution I need. But 
there are two things which I am not able get. I am new to ansible, kindly 
answer those question even though it was silly. whether the variables which 
are being used in setfact need to be set before using that in setfact, if 
so how to do that. And the 2nd question is 

- set_fact: 
      my_env: "{{ envs|default({}) | combine( { item.split('=')[0]: 
item.split('=')[1] } ) }}" 
    loop: "{{ dotenv_pairs.split() }}" 

in the above task what is envs and what is the roll of envs|default({})

On Thursday, 20 February 2020 19:24:03 UTC+5:30, Dick Visser wrote:
>
> Hi 
>
> You could fetch the .env file, parse it, and then use that as 
> environment vars for a subsequent shell/command. 
> It's not the cleanest method and probably fragile. 
>
> There is another problem, the shell and command module will execute a 
> single command. 
> What you have is two command, the first one being "su". 
> Instead you should either connect as the user you want to run the 
> command as, or you should use proper privilege escalation. 
> Read 
> https://docs.ansible.com/ansible/latest/user_guide/become.html#understanding-privilege-escalation-become
>  
> for that. 
> If "<id>" is different that the user you connect as, but it's not a 
> privileged user, you will run into issues, so for that read: 
>
> https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-of-becoming-an-unprivileged-user
>  
>
> Having said all of that, this should do the trick: 
>
>
> --- 
> - hosts: dev 
>   gather_facts: false 
>   tasks: 
>   - name: Slurp .env 
>     slurp: 
>       src: /path/to/.env 
>     register: dotenv 
>
>   - set_fact: 
>       dotenv_pairs: "{{ dotenv.content | b64decode | 
> regex_replace('export\\s+', '') }}" 
>
>   - set_fact: 
>       my_env: "{{ envs|default({}) | combine( { item.split('=')[0]: 
> item.split('=')[1] } ) }}" 
>     loop: "{{ dotenv_pairs.split() }}" 
>
>   - name: Execute with custom environment vars 
>     shell: echo $HOME $MYVAR 
>     environment: "{{ my_env }}" 
>     become: yes 
>     become_user: <id> 
>
>
>
> On Thu, 20 Feb 2020 at 13:18, Sivaraman P <[email protected] 
> <javascript:>> wrote: 
> > 
> > Hi, Thank you for your response, 
> > 
> > I have a command to check the status of abinitio service in target 
> machines. But, the status command will only work if the env file is 
> executed. 
> > 
> > The env file contain some environment variables like below. The 
> variables numbers and value will change for one target machine to another. 
> > 
> > export AB_HOME=/et/dev/abinitio/abinitio-V3 
> > export PATH=${AB_HOME}/bin:${PATH} 
> > 
> > I tried to execute the env file present in the target machine by 
> sourcing the env file and I tried to register the output to get what are 
> all the variables present in the file in-order to use them in my next task, 
> which is to check the status. But,  I am able to see my register variable 
> is empty.  I just want to execute the environment file and to run the 
> status command with that executed environment. Below is the playbook I have 
> used, 
> > 
> > - hosts: dev 
> >   gather_facts: false 
> >   tasks: 
> >     - name: get the environment variables 
> >       shell: "su <id> & . ./.env" #this command is to execute the env 
> file and store the result 
> >       args: 
> >         chdir: /path to the file 
> >       register: output1 #for storing exported variables. 
> > 
> >     - debug: var=output1.stdout_lines  #print exported variables (Here I 
> am not getting anything the stdout is empty) 
> > 
> > 
> > 
> > 
> > On Thursday, 20 February 2020 17:10:23 UTC+5:30, Dick Visser wrote: 
> >> 
> >> Hi 
> >> 
> >> Could you please tell clearly: 
> >> 
> >> - What you are trying to achieve. 
> >> - How you are doing this. 
> >> - What problems you encounter. 
> >> - Which command did you run, and what output did you get from that 
> >> (copied as text, not as images/attachments/screenshots) 
> >> - What the relevant tasks/playbooks/code/variables look like 
> >> 
> >> 
> >> 
> >> 
> >> On Thu, 20 Feb 2020 at 10:55, Sivaraman P <[email protected]> wrote: 
> >> > 
> >> > 0I 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 as well. 
> >> > 
> >> > - 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 
> > 
> > 
> >           - name: check status 
> >             shell: "su <id> & <status checking command>" #command to 
> check status 
> >             environment: "{{output1.stdout_lines}}" #setting the 
> environment in which th above status command need to run 
> >             register: output2 
> > 
> >           - debug: var=output2.stdout_lines 
> > 
> >> 
> >> > 
> >> > -- 
> >> > You received this message because you are subscribed to the Google 
> Groups "Ansible Project" group. 
> >> > To unsubscribe from this group and stop receiving emails from it, 
> send an email to [email protected]. 
> >> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/11783c30-108d-406b-8461-bfc9b38eb4c3%40googlegroups.com.
>  
>
> >> 
> >> 
> >> 
> >> -- 
> >> Dick Visser 
> >> Trust & Identity Service Operations Manager 
> >> GÉANT 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "Ansible Project" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to [email protected] <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/49eba48c-3b9e-4052-a445-25a628ef3325%40googlegroups.com.
>  
>
>
>
>
> -- 
> Dick Visser 
> Trust & Identity Service Operations Manager 
> GÉANT 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c5e11ead-1392-4973-bf86-de2f8f473a7f%40googlegroups.com.

Reply via email to