I need to export some environment variables before running a task, but it 
appears that setting variables with "environment" does not export them; 
they remain local.  For test, I wrote the following simple playbook:

---
- hosts: localhost
  gather_facts: no
  environment:
      MYVAR: "TEST1"

  tasks:
    - shell: echo "$MYVAR"
      register: mytest

    - debug:
        msg:
          - "Ansible MYVAR = {{mytest.stdout}}"
          - "Exported MYVAR = {{lookup('env', 'MYVAR')}}"


And when I run it with the shell MYVAR set, I get the following results:

export MYVAR=TEST2;ansible-playbook ~/devansible/playbooks/mytest

PLAY [localhost] 
***************************************************************

TASK [shell] 
*******************************************************************
changed: [localhost]

TASK [debug] 
*******************************************************************
ok: [localhost] =>
  msg:
  - Ansible MYVAR = TEST1
  - Exported MYVAR = TEST2

PLAY RECAP 
*********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    
failed=0    skipped=0    rescued=0    ignored=0


So, "environment" is not exporting the value as I expected.
The main issue I have is that I am trying to set SSH_AUTH_SOCK so that 
subsequent tasks can be executed on remote servers.  In my playbook, I 
start ssh-agent locally, grab the SSH_AUTH_SOCK and SSH_AGENT_PID values, 
and place them into the Ansible environment.  During the next task, I use 
ssh-add to add a SSH key to the now running agent.  That works perfectly, 
and in fact I can query the running agent from outside of Ansible and see 
the SSH key that was added inside of the playbook.  But any remote tasks I 
try to execute after adding the SSH key to the agent fail with 
"UNREACHABLE".  Apparently the remote tasks are using the original shell 
version of SSH_AUTH_SOCK to connect to ssh-agent, not the Ansible version.  
The Ansible version of SSH_AUTH_SOCK needs to be exported first.
Is there a way to actually export variables from within Ansible?  I can 
always start ssh-agent prior to running Ansible so that the SSH_AUTH_SOCK 
environment variable will contain the correct path to the agent socket, but 
I was trying to avoid doing that for various reasons; I want to try to keep 
all of the steps, including starting and stopping the SSH agent, contained 
within a single playbook.
Is there a flag set or something I can do that will cause the variables to 
be truly exported?





 

-- 
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/1a126537-a388-4a2a-81dc-108aa74ef27d%40googlegroups.com.

Reply via email to